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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$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,有两个地方:
1
2
$mail->SMTPSecure = 'ssl';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
3、密码我使用的是授权码:

 

 

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

 

posted @   蓝色星辰1993  阅读(643)  评论(1编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示