0.从qq获取授权码
1.下载
pnpm i nodemailer
2.service层
async SendEmailByUserNameByUserPasswordByUserEmail(user){
const nodemailer = require('nodemailer');
const user_email = 'abcd.com'; //这里写发送者的qq号
const auth_code = 'abcd'; //这里是qq授权码
/*————————————————————————————————————————————*/
const transporter = nodemailer.createTransport({ //初始化区块 //不用动
service: 'qq',
secureConnection: true,
port: 465,
auth: {
user: user_email, // 账号
pass: auth_code, // 授权码
},
});
/*—————————————————————————————————————————————*/
const verificationCode = Math.floor(Math.random() * 900000 + 100000);
const mailOptions = { //定义基础配置
from: user_email, // 发送者,与上面的user一致
to: user.UserEmail, // 接收者,可以同时发送多个,以逗号隔开
subject: "我是标题", // 标题
html: `这是验证码:${verificationCode}`
};
/*——————————————————————————————————————————————————*/
let result;
try {
// 发送邮件
await transporter.sendMail(mailOptions);
result = {
code: 0,
msg: '验证码已发送',
}
} catch (error) {
console.log(error)
}
return result;
}
3.controller层
async SendEmailByUserNameByUserPasswordByUserEmail() {
const { ctx } = this;
const result = await this.ctx.service.userService.SendEmailByUserNameByUserPasswordByUserEmail(this.ctx.request.body);
ctx.body = result
}
4.路由层
router.post("/sendmail" , controller.userController.SendEmailByUserNameByUserPasswordByUserEmail)
标签:qq,const,nodemailer,js,result,SendEmailByUserNameByUserPasswordByUserEmail,egg,u
From: https://blog.csdn.net/m0_53785610/article/details/137023731