【PHP】PHP Mailer 发送邮件采坑记录

项目需要新开发一个发邮箱功能,以前做过,以为信手拈来,没想到花了两个小时,记录踩的坑

        $mail = new PHPMailer();
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;               // Enable verbose debug output
        $mail->isSMTP();                                         // Send using SMTP
        $mail->SMTPSecure = 'ssl';
        $mail->Host       = 'smtp.163.com';                 // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                  // Enable SMTP authentication
        $mail->Username   = 'xxx@163.com';                         // SMTP username
        $mail->Password   = '邮箱开启SMTP后的授权码';               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;          // Enable TLS encryption; 
        $mail->Port       = 465;                                     // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
        
        $mail->setFrom('dongfanglong1993@163.com', 'Heyw');
        $mail->addAddress('xxx@qq.com', 'Lanse93');               // Add a recipient
        $mail->addAttachment('./upload/contract/1.docx');         	// Add attachments
        
        $mail->Subject = '邮箱标题';
        $mail->Body    = '邮箱内容';
        $mail->send();

  

这段代码是可以直接执行的,遇到的问题主要是connect time out,通过以下方法处理

1、isSMTP里面讲smtp小写改成SMTP大写(测试无效,只做记录)
2、开启ssl,有两个地方:
$mail->SMTPSecure = 'ssl';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
3、密码我使用的是授权码:

发送邮箱的前提,是需要有个发送的账号,需要开启SMTP服务,开启后会有一个授权码,有些邮箱服务账号认证是需要使用授权码的(我使用的就是授权码发送)

得意时做事,失意时读书
原文地址:https://www.cnblogs.com/lanse1993/p/13156075.html