<?php
include("class/class.phpmailer.php"); //下載phpmailer並include兩個文件
include("class/class.smtp.php");
$mail = new PHPMailer(); //得到一個PHPMailer實例
$mail->CharSet = "utf-8"; //設置采用utf-8中文編碼(內容不會亂碼)
$mail->IsSMTP(); //設置采用SMTP方式發送郵件
$mail->Host = "smtp.qq.com"; //設置郵件服務器的地址(若為163郵箱,則是smtp.163.com)
$mail->Port = 25; //設置郵件服務器的端口,默認為25
$mail->From = "發件人"; //設置發件人的郵箱地址
$mail->FromName = "收件人姓名"; //設置發件人的姓名(可隨意)
$mail->SMTPAuth = true; //設置SMTP是否需要密碼驗證,true表示需要
$mail->Username="發件人"; (后面有解釋說明為何設置為發件人)
$mail->Password = "發件人郵箱密碼";
$mail->Subject = "你好啊"; //主題
$mail->AltBody = "text/html"; // optional, comment out and test
$mail->Body = "你的郵件的內容"; //內容
$mail->IsHTML(true);
//$mail->WordWrap = 50; //設置每行的字符數
$mail->AddReplyTo("回復地址","from"); //設置回復的收件人的地址(from可隨意)
$mail->AddAddress("收件人","to"); //設置收件的地址(to可隨意)
echo $mail->Send();
?>
若出現不能連接或者無法通過驗證,則
1、在class.phpmailer.php中
363-365行,將smtp小寫改成大寫
public function IsSMTP() {
$this->Mailer = 'SMTP';
}
2、572-579行,將case "smtp"小寫改成大寫
switch($this->Mailer) {
case 'sendmail':
return $this->SendmailSend($header, $body);
case 'SMTP':
return $this->SmtpSend($header, $body);
default:
return $this->MailSend($header, $body);
}
3、出現錯誤無法通過驗證時,是因為在811-817中
$connection = true;
if ($this->SMTPAuth) {
if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
throw new phpmailerException($this->Lang('authenticate'));
}
}
}
驗證時調用的是Username和Password,所以設置時將Username與發件人的from設置相同,同時設置為發件人郵箱即可解決通過。