PHPMailer

先由右方取得PHPMailer,下載完畢後解壓再把class.phpmailer.php、class.smtp.php二個類別檔拷貝出來點我下載

使用前注意事項

  1. 使用PHPmailer時,PHP 必須安裝 OpenSSL 的擴充程式。 最好使用php 5以上,因為php 4 ,有OpenSSL的bug…,然後phpmailer 使用2.1 以上版本for php 5/6。關於openssl的安裝方式可以參考一下這篇文章 「[php安裝openssl的方法](/href="http://blog.yogo.tw/2009/08/phpopenssl.html)」。

  2. 使用 PHPMailer 透過 Gmail 帳號寄信時,需要採用安全性較低的登入技術,所以要開啟安全性較低的應用程式存取權限,您可以在登入 Gmail 帳號後由https://www.google.com/settings/security/lesssecureapps開啟,否則會造成「send-mail: Authorization failed 534 5.7.14」認證錯誤。

  3. 開發測試時建議您可以開啟 PHPMailer 的 Debug 模式,可以很清楚知道運作的過程,若發生錯誤也可以很快得知問題所在,其語法如下:

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

這邊只提供PHP需要設定的地方的code

include("class.phpmailer.php"); //匯入PHPMailer類別

$Name=$_POST['sndname'];
$Mail=$_POST['sendmail'];
$Subject=$_POST['subject'];
$Sendbody=$_POST['sendbody'];

$mail= new PHPMailer(); //建立新物件
$mail->IsSMTP(); //設定使用SMTP方式寄信
$mail->SMTPAuth = true; //設定SMTP需要驗證
$mail->SMTPSecure = "ssl"; // Gmail的SMTP主機需要使用SSL連線
$mail->Host = "smtp.gmail.com"; //Gamil的SMTP主機
$mail->Port = 465;  //Gamil的SMTP主機的埠號(Gmail為465)。
$mail->CharSet = "utf-8"; //郵件編碼

$mail->Username = "xxxx@gmail.com"; //Gamil帳號
$mail->Password = "password"; //Gmail密碼

$mail->From = $Mail; //寄件者信箱
$mail->FromName = "線上客服"; //寄件者姓名

$mail->Subject ="一封線上客服信";  //郵件標題
$mail->Body = "姓名:".$Name."信箱:".$Mail."主題:".$Subject."回應內容:".$Sendbody; //郵件內容

$mail->IsHTML(true); //郵件內容為html ( true || false)
$mail->AddAddress("who@mail.com.tw"); //收件者郵件及名稱

if(!$mail->Send()) {
    echo "發送錯誤: " . $mail->ErrorInfo;
} else {
    echo "感謝您的回覆,我們將會盡速處理!";
}

Last updated