1、开启服务
2、检测是否开启php_openssl
3、发送邮件服务器:smtp.qq.com,使用SSL,端口号465或587
4、密码,最主要是这个,不是填写QQ密码也不是QQ邮箱的密码,而是在开启POP3或SMTP的时候给的授权码。忘记了授权码的可以先关闭再点开启重新获取
完整实例
/**
* 邮件发送
* @param $to 接收人
* @param string $subject 邮件标题
* @param string $content 邮件内容(html模板渲染后的内容)
* @throws Exception
* @throws phpmailerException
*/
function sendEmail($to, $subject = '', $content = '')
{
//判断openssl是否开启
$openssl_funcs = get_extension_funcs('openssl');
if (!$openssl_funcs) {
return array('code' => 0, 'msg' => '请先开启openssl扩展');
}
//获取邮件配置
$config = config('smtp_email');
$config['email_host'] = 'smtp.qq.com';
$config['email_port'] = 465;
$config['email_secure'] = 'ssl'; #ssl tls
$config['email_addr'] = '*****@qq.com';
$config['email_pass'] = '****vdtacbdc';
$mail = new PHPMailer;
#phpmailer SMTP ERROR: Failed to connect to server: (0) 解决办法
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
),
);
$mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->isSMTP();
// Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug =0;
//调试输出格式
$mail->Debugoutput = 'html';
//smtp服务器
$mail->Host = $config['email_host'];
//端口 - likely to be 25, 465 or 587
$mail->Port = $config['email_port'];
$mail->SMTPSecure = $config['email_secure']; // 使用安全协议
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
#设置发件人昵称
$mail->FromName = '测试';
//用户名
$mail->Username = $config['email_addr'];
//密码
$mail->Password = $config['email_pass'];
//Set who the message is to be sent from
$mail->setFrom($config['email_addr']);
// 回复地址
// $mail->addReplyTo('replyto@example.com', 'First Last');
// 接收邮件方
if (is_array($to)) {
foreach ($to as $v) {
$mail->addAddress($v);
}
} else {
$mail->addAddress($to);
}
$mail->isHTML(true); // send as HTML
// 标题
$mail->Subject = $subject;
// 邮箱正文
$mail->Body = $content;
// HTML内容转换
// $mail->msgHTML($content);
// Replace the plain text body with one created manually
// $mail->AltBody = 'This is a plain-text message body';
// 添加附件
// $mail->addAttachment('images/phpmailer_mini.png');
// send the message, check for errors
if(!$mail->send()){
return array('code' => 0, 'msg' => $mail->ErrorInfo);
}
return array('code' => 1, 'msg' => '成功');
}