August 18, 2007
在windows寫PHP程式時,若要從程式中寄信出去,必須在php.ini做好SMTP設定才行。但不是每個人都有現成可用的SMTP可設定,有些SMTP是需要認證的,那就沒轍了!因此,若是能夠利用人人可申請的Gmail的SMTP來寄信就太美好了!終於,在網路上找到了個好方法,經過一番測試後,確實可以使用。
註:本文對正常人而言可能超無聊,對我而言則是個寶囉~所以特別記錄下來,日後可能用得上啊!
先到 http://sourceforge.net/projects/phpmailer 下載phpmailer 1.73
然後依據 http://www.fwolf.com/blog/post/155 的方法,在class.phpmailer.php的544行加入:
//Modify by Fwolf @ 2006-4-14, to enable ssl mail connection
$host = $this->Host;
$port = $this->Port;
然後,再開啟class.smtp.php,於1024行的fgets前加入「@」以隱藏錯誤訊息,至此,程式修改完成!
由於Gmail寄信是用SSL協定,所以根據http://tw2.php.net/manual/en/ref.openssl.php的說法,要讓PHP的openssl啟動,需要
最後只要把程式部份寫好就可以用啦~
<?php
$to_address="aaa@gmail.com";
$to_name="aaa";
$subject="使用phpmailer發送郵件";
$body="使用phpmailer發送郵件也蠻辛苦的~";
send_mail($to_address, $to_name ,$subject, $body);
function send_mail($to_address, $to_name ,$subject, $body, $attach = "")
{
//使用phpmailer發送郵件
require_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->CharSet = "Big5";
$mail->Encoding = "base64";
$mail->From = "寄件人Email";
$mail->FromName = "寄件人";
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465; //default is 25, gmail is 465 or 587
$mail->SMTPAuth = true;
$mail->Username = "帳號";
$mail->Password = "密碼";
$mail->addAddress($to_address, $to_name);
$mail->WordWrap = 50;
if (!empty($attach))
$mail->AddAttachment($attach);
$mail->IsHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send())
{
echo "郵件送出失敗!\r\n";
echo "錯誤訊息: " . $mail->ErrorInfo . "\r\n";
return false;
}
else
{
echo("寄信 $attach 給 $to_name <$to_address> 完成!\r\n");
return true;
}
}
?>
註:本文對正常人而言可能超無聊,對我而言則是個寶囉~所以特別記錄下來,日後可能用得上啊!
先到 http://sourceforge.net/projects/phpmailer 下載phpmailer 1.73
然後依據 http://www.fwolf.com/blog/post/155 的方法,在class.phpmailer.php的544行加入:
//Modify by Fwolf @ 2006-4-14, to enable ssl mail connection
$host = $this->Host;
$port = $this->Port;
然後,再開啟class.smtp.php,於1024行的fgets前加入「@」以隱藏錯誤訊息,至此,程式修改完成!
由於Gmail寄信是用SSL協定,所以根據http://tw2.php.net/manual/en/ref.openssl.php的說法,要讓PHP的openssl啟動,需要
複製ssleay32.dll 及 libeay32.dll 到system目錄,然後將php.ini中的openssl註解取消。如此,PHP便能支援openssl。最後只要把程式部份寫好就可以用啦~
<?php
$to_address="aaa@gmail.com";
$to_name="aaa";
$subject="使用phpmailer發送郵件";
$body="使用phpmailer發送郵件也蠻辛苦的~";
send_mail($to_address, $to_name ,$subject, $body);
function send_mail($to_address, $to_name ,$subject, $body, $attach = "")
{
//使用phpmailer發送郵件
require_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->CharSet = "Big5";
$mail->Encoding = "base64";
$mail->From = "寄件人Email";
$mail->FromName = "寄件人";
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465; //default is 25, gmail is 465 or 587
$mail->SMTPAuth = true;
$mail->Username = "帳號";
$mail->Password = "密碼";
$mail->addAddress($to_address, $to_name);
$mail->WordWrap = 50;
if (!empty($attach))
$mail->AddAttachment($attach);
$mail->IsHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send())
{
echo "郵件送出失敗!\r\n";
echo "錯誤訊息: " . $mail->ErrorInfo . "\r\n";
return false;
}
else
{
echo("寄信 $attach 給 $to_name <$to_address> 完成!\r\n");
return true;
}
}
?>




