const nodemailer = require('nodemailer'); //引入模块 const reg = /^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/ const { EMAILCONFIG,SYSTEM_NAME } = require('../config/default.config') const transporter = nodemailer.createTransport({ //node_modules/nodemailer/lib/well-known/services.json 查看相关的配置,如果使用qq邮箱,就查看qq邮箱的相关配置 host:EMAILCONFIG.HOST, // secureConnection:true, service: 'qq', //类型qq邮箱 // port: 465, secure: true, // true for 465, false for other ports auth: { user: EMAILCONFIG.USER, // 发送方的邮箱 pass:EMAILCONFIG.PASS// smtp 的授权码 } //pass 不是邮箱账户的密码而是stmp的授权码(必须是相应邮箱的stmp授权码) //邮箱---设置--账户--POP3/SMTP服务---开启---获取stmp授权码 }); const typeList = { signUp:'账户注册', signIn:'登录', reset:'重置密码', writeoff:'注销账户' } function sendMail(mail, code, type) { console.log(mail, code) return new Promise((rec,rej) => { if(!checkEmail(mail)){ rej('邮箱格式错误!') } // 发送的配置项 const mailOptions = { from: '"TOGY.GC" ', // 发送方 to: mail, //接收者邮箱,多个邮箱用逗号间隔 subject: `${SYSTEM_NAME}!`, // 标题 text: 'Hello world?', // 文本内容 html: `

欢迎使用 ${SYSTEM_NAME}

您在某些地方请求了邮箱的验证码,如果不是自己操作请修改账户的密码。

此验证码用于 ${typeList[type]}。

${code}
`, //页面内容 // attachments: [{//发送文件 // filename: 'index.html', //文件名字 // path: './index.html' //文件路径 // }, // { // filename: 'sendEmail.js', //文件名字 // content: 'sendEmail.js' //文件路径 // } // ] }; //发送函数 transporter.sendMail(mailOptions, (error, info) => { if (error) { rej(error,info) } else { rec(info) //因为是异步 所有需要回调函数通知成功结果 } }); }) } function checkEmail(email){ return reg.test(email) } module.exports = { sendMail, checkEmail }