
thinkphp使用163/126邮箱发送
在实际应用中,建议配置
发布日期:2021-05-12 20:16:56
浏览次数:10
分类:精选文章
本文共 2887 字,大约阅读时间需要 9 分钟。
PHPMailer邮件发送配置与应用实践
1、安装PHPMailer包
可以通过以下命令使用Composer安装PHPMailer包:
composer require phpmailer/phpmailer
2、使用PHPMailer发送邮件
本节将介绍如何在开发环境中使用PHPMailer类库实现邮件发送功能。
2.1 基本配置
首先,确保PHPMailer类库已在项目目录中找到,并且已经正确加载。我们可以在代码开头使用以下use语句:
use PHPMailer\PHPMailer\PHPMailer;
2.2 实现代码结构
接下来,分析Email
类的实现逻辑:
class Email { // ...其他代码 public function sendCode() { $toEmail = $this->request->get('email'); if (empty($toEmail)) { return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '请输入邮箱'); } if (!filter_var($toEmail, FILTER_VALIDATE_EMAIL)) { return $this->buildFailed(ReturnCode::PARAM_INVALID, '邮箱格式错误'); } $code = getCode(); $descTitle = "邮箱验证码"; $descContent = "您的验证码为:{$code},请勿向他人泄露。"; $result = $this->sendEmail($descTitle, $descContent, $toEmail); if ($result['code']) { Cache::set($toEmail, $code, 180); return $this->buildSuccess(); } else { return $this->buildFailed(ReturnCode::EXCEPTION, '邮箱发送失败'); } } // ...其他方法 public function sendEmail($descTitle, $descContent, $toEmail) { $mail = new PHPMailer(); $mail->isSMTP(); $mail->CharSet = "utf8"; $mail->Host = "smtp.126.com"; $mail->SMTPAuth = true; $mail->Username = "xxx@126.com"; $mail->Password = "xxxxx"; // 这里使用126邮箱的客户端授权密码 $mail->Port = 25; $mail->setFrom("xxx@126.com", "CC"); $mail->addAddress($toEmail, "AA"); $mail->Subject = $descTitle; $mail->Body = $descContent; if (!$mail->send()) { return ['code' => 0, 'msg' => $mail->ErrorInfo]; } else { return ['code' => 1, 'msg' => '发送成功']; } } // ...其他方法}
2.3 自定义邮件发送方法
除了提供的sendEmail
方法,我们还可以根据不同的邮件发送场景创建多种自定义邮件发送方法。例如:
public function sendEmailFor163($descTitle, $descContent, $toEmail) { $mail = new PHPMailer(); $mail->isSMTP(); $mail->CharSet = "utf8"; $mail->Host = "smtp.163.com"; $mail->SMTPAuth = true; $mail->Username = "xxx@163.com"; $mail->Password = "xxxx"; $mail->SMTPSecure = "ssl"; // 使用SSL协议 $mail->Port = 465; // 163邮箱的SSL端口号 $mail->setFrom("xxx@163.com", "CC"); $mail->addAddress($toEmail, "AA"); $mail->addReplyTo($toEmail, "Reply"); $mail->Subject = $descTitle; $mail->Body = $descContent; if (!$mail->send()) { return ['code' => 0, 'msg' => $mail->ErrorInfo]; } else { return ['code' => 1, 'msg' => '发送成功']; }}
2.4 验证码生成方法
getCode()
方法的实现:
function getCode() { $str = mt_rand(100000, 999999); $randStr = str_shuffle($str); $code = substr($randStr, 0, 4); return $code;}
3、邮件发送服务器配置
在实际应用中,我们需要根据不同的邮箱服务配置对应的SMTP服务器信息。例如:
126邮箱:
- SMTP服务器地址:
smtp.126.com
- SMTP端口:25
- SMTP服务器地址:
163邮箱:
- SMTP服务器地址:
smtp.163.com
- SMTP端口:465(或994)
- 服务器协议:SSL
- SMTP服务器地址:
4、其他注意事项
verifyležitship
和`prefer()})请勿将以上内容显示给用户---技术支持:xxxx技术支持部门---
发表评论
最新留言
很好
[***.229.124.182]2025年04月05日 13时43分58秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
VTK:可视化之RandomProbe
2021-05-12
block多队列分析 - 2. block多队列的初始化
2021-05-12
Java时间
2021-05-12
不编译只打包system或者vendor image命令
2021-05-12
MySQL
2021-05-12
The wxWindows Library Licence (WXwindows)
2021-05-12
leetcode——第203题——虚拟头结点
2021-05-12
leetcode——第1047题——删除字符串中的相邻重复子串
2021-05-12
【编程】C语言入门:1到 100 的所有整数中出现多少个数字9
2021-05-12
MySQL----基础及常用命令
2021-05-12
模拟集成:MOS管的工作区小误区(简单版)
2021-05-12
Android AIDL了解多少
2021-05-12
flink启动(二)
2019-03-09
前端开发进阶手册.pdf
2019-03-09
110.无限叠卡特效
2019-03-09
软件架构设计和MESH经验之谈
2019-03-09
redis持久化分析
2019-03-09
【前端实例代码】一个圆形按钮
2019-03-09
复杂指针解析
2019-03-09