当前位置:网站首页>TP5 发送邮件(2020-05-27)
TP5 发送邮件(2020-05-27)
2022-04-22 06:34:00 【阿呆小跟班】
1、邮箱开通SMTP服务
2、git下载扩展 phpmailer
https://github.com/PHPMailer/PHPMailer
放入TP vendor 目录下(主要是PHPMailer.php和SMTP.php两个文件)

- 注意:PHPMailer.php 和 SMTP.php 文件的命名空间得是

2、common.php 创建发送邮件公共方法
1)先use

2)发送邮件方法代码
function sendEmail($data = [])
{
// 引入这两行,否则报错
Vendor('phpmailer.PHPMailer');
Vendor('phpmailer.SMTP');
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); //启用SMTP
$mail->Host = 'smtp.qq.com'; //SMTP服务器 以126邮箱为例子
$mail->Port = 465; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->SMTPSecure = "ssl"; // 设置安全验证方式为ssl
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = '[email protected]'; //你的邮箱
$mail->Password = 'xxxxxxxxxxx'; //你开通SMTP服务时的密码
$mail->Subject = 'xxxx'; //邮件标题
$mail->From = '[email protected]'; //发件人地址(你的邮箱地址)
$mail->FromName = '小李'; //发件人姓名
if ($data && is_array($data)) {
foreach ($data as $k => $v) {
$mail->AddAddress($v['user_email'], "昵称"); //添加收件人(地址,昵称)
$mail->IsHTML(true); //支持html格式内容
$mail->Body = $v['content']; //邮件主体内容
//发送成功就删除
if ($mail->Send()) {
echo "发送成功";
} else {
echo "Mailer Error: " . $mail->ErrorInfo; // 输出错误信息
}
}
}
}
3、控制器层使用
// 用户注册发送邮箱验证码
public function sendMailCode()
{
// 用户邮箱
$userMail = input('userMail','');
// 随机生成客户代码,8位
$chars = "0123456789";
$length = 6;
$vipCode = '';
for($i = 0; $i < $length; $i++){
$vipCode .= $chars{
mt_rand(0,9)};
}
// 将验证码存入session
Session::set('validate',$vipCode);
sendEmail([['user_email'=>$userMail, 'content'=>'【验证码:(请在2分钟内填写)】' . $vipCode]]);
return json(['code' => 0, 'msg' => '获取验证码成功']);
}
版权声明
本文为[阿呆小跟班]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43295023/article/details/106376160
边栏推荐
- .Net5 Log4Net启动一段时间后记录日志到数据库中失败问题
- jmeter 参数请求类型
- 职场小白,频繁问问题会不会被嫌弃?
- 实验5 组件及事件处理
- Workplace white, will you be despised for asking questions frequently?
- 培训赋能 | 打造专业技术服务团队
- Postman interface automation-4-tests assertion 4: custom interface execution order
- web自动化:4.2selenium如何实现iframe窗口的切换(3种方法)
- jmeter_mysql 数据库连接
- Understanding of transformer mechanism
猜你喜欢

SuperSocket在.Net5中使用——WebSocket Server篇

Raspberry Pie: adafruit's I2C ssd1306 OLED driver

八阿哥纪事【四】

Web课设----HIS系统

Shiping information was listed in the panorama of China's network security market in 2021

数据库原理课程设计——物资发放管理系统

Figure 2022 work plan of industrial Internet special working group

ik分词器-简单使用

log4net在.Net Core使用

聚焦科技创新 | 世平信息亮相科创中国技术路演——信息技术与安全专场
随机推荐
Shiping information has successfully passed the evaluation of "capability verification of data security products" of China Academy of communications and communications
web自动化:5.1selenium鼠标操作-单击、双击、右击、悬停、拖拽
Shiping information has successfully passed CMMI Level 3 certification
配置表及页面信息 自动生成CURD操作页面
Shiping information appeared at the China traditional Chinese medicine information conference to help build the data security system of the pharmaceutical industry
Bert model series 1: what exactly is input?
echars实现自定义山西省地图和地图下钻返回
CefSharp存储Cookie和读Cookie
Shrio 学习笔记(二)
Training and empowerment | building a professional technical service team
The data security product guide was officially released, and many products of Shiping information were included
Use SSS framework to build a simple dormitory management system
SuperSocket在.Net5中使用——概念篇
初级测试:普通 VS 优秀
pytest_第一节课
jmeter 接口请求出现安全验证解决方案
Web automation: 4.2 selenium how to switch iframe windows (3 methods)
How to connect Kunlun on state │ G series screen with Siemens 300 domestic MPI adapter
Charles使用之修改请求和响应的三种方式
Supersocket is Used in net5 - appsession and supersocketservice