• 【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服务,开启后会有一个授权码,有些邮箱服务账号认证是需要使用授权码的(我使用的就是授权码发送)

    得意时做事,失意时读书
  • 相关阅读:
    Framework 4.0 新关键字dynamic 之我见(二)
    随便歇歇
    最近的一些总结
    一周最新示例代码回顾 (7/16 7/22)
    一周最新示例代码回顾 (5/28–6/3)
    一周最新示例代码回顾 (5/14–5/20)
    一周最新示例代码回顾 (6/25 7/1)
    微软一站式示例代码浏览器本周更新发布
    一周最新示例代码回顾 (6/11 6/17)
    示例代码浏览器5.4功能更新
  • 原文地址:https://www.cnblogs.com/lanse1993/p/13156075.html
Copyright © 2020-2023  润新知