前言:
首先,我们在确保前面都没有配置问题的时候,在本地是可以跑,但是在阿里服务器上就不可以跑的问题,在阿里服务器默认25端口是封掉的,我们只能通过更换端口解决问题。
方法:
phpmailer 只要不使用25端口smtp发邮件就报错
因为更换25端口后其他端口,会自动进行加密,因此需要配置SSL
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = file_get_contents('contents.html');
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->SMTPDebug = 2;
// 邮件正文为html编码
$mail->Debugoutput = 'html';
// 使用smtp鉴权方式发送邮件
$mail->isSMTP();
// smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
// 链接qq域名邮箱的服务器地址
$mail->Host = "smtp.qq.com";
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // set the SMTP server port
$mail->Host = "smtp.163.com"; // SMTP server
$mail->Username = ".."; // SMTP server username
$mail->Password = ".."; // SMTP server password
//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("QWEQWE@163.com","First Last");
$mail->From = "QWEQWEWQE@163.com";
$mail->FromName = "First Last454654";
$to = "QWEQWE@qq.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
在原先的基础上我们添加
$mail->SMTPDebug = 2;
// 邮件正文为html编码
$mail->Debugoutput = 'html';
// 使用smtp鉴权方式发送邮件
$mail->isSMTP();
// smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
然后就没问题了....
百度的方法真是迷人................................