php 使用 phpmailer 发送邮件代码如下
include_once ("phpmailer/class.phpmailer.php");
include_once ("phpmailer/class.smtp.php");
$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
$address = $email;
$mail->IsSMTP();
$mail->Host = "smtp.exmail.qq.com";
$mail->SMTPAuth = true;
$mail->Username = "bcxw@bcxw.net";
$mail->Password = "xxxxxxxxx";
$mail->Port = 465;
$mail->From = "admin@bcxw.net";
$mail->FromName = "编程学问网";
$mail->AddAddress("$address", "$address");
$mail->IsHTML(true);
$mail->Subject = "编程学问网-邮箱验证";
$mail->Body = "您好!欢迎访问 编程学问网";
if (!$mail->Send()) {
$result["code"]=1;
$result["message"]="邮箱验证码发送失败,请稍后再试!";
}
但是报错:
<br />
<b>Notice</b>: fputs() [<a href='function.fputs'>function.fputs</a>]: send of 15 bytes failed with errno=32 Broken pipe in <b>/data/home/qxu1591320166/htdocs/user/phpmailer/class.smtp.php</b> on line <b>494</b><br />
<br />
<b>Notice</b>: fputs() [<a href='function.fputs'>function.fputs</a>]: send of 12 bytes failed with errno=32 Broken pipe in <b>/data/home/qxu1591320166/htdocs/user/phpmailer/class.smtp.php</b> on line <b>212</b><br />
<br />
<b>Fatal error</b>: Uncaught exception 'phpmailerException' with message 'SMTP Error: Could not authenticate.' in /data/home/qxu1591320166/htdocs/user/phpmailer/class.phpmailer.php:814
Stack trace:
#0 /data/home/qxu1591320166/htdocs/user/phpmailer/class.phpmailer.php(705): PHPMailer->SmtpConnect()
#1 /data/home/qxu1591320166/htdocs/user/phpmailer/class.phpmailer.php(576): PHPMailer->SmtpSend('Date: Wed, 5 Fe...', '???????????????...')
#2 /data/home/qxu1591320166/htdocs/user/userManage.php(105): PHPMailer->Send()
#3 {main}
thrown in <b>/data/home/qxu1591320166/htdocs/user/phpmailer/class.phpmailer.php</b> on line <b>814</b><br />
解决方法:
查看了腾讯企业邮箱smtp配置方法
https://service.exmail.qq.com/cgi-bin/help?subtype=1&id=28&no=1000585
POP3/SMTP协议
接收邮件服务器:pop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465
说是要使用ssl,所以添加下面代码立马解决:
$mail->SMTPSecure = 'ssl';