参考 https://nodemailer.com/about/
安装
npm install nodemailer --save
sendEmail.js
const nodemailer = require('nodemailer');
const config = {
email:{
host: 'smtp.exmail.qq.com',
port: 465,
secure: true,
user: 'example@qq.com',
pass: 'UZNmJxTgbekA949o',
from: 'example@qq.com',
subject: '您的密码重置完成',
},
};
async function sendEmail(address,passwordInfo) {
let account = await nodemailer.createTestAccount();
console.log('Credentials obtained, sending message...');
// Create a SMTP transporter object
let transporter = nodemailer.createTransport(
{
host: config.email.host,
port: config.email.port,
secure: config.email.secure,
auth: {
user: config.email.user,
pass: config.email.pass
},
logger: false,
debug: false
},
{
from: config.email.from,
}
);
let info = await transporter.sendMail(
{
to: address,
subject: config.email.subject,
text: `新密码为:${passwordInfo}`,
html: ''
}
);
console.log('Message sent successfully!');
console.log('Server responded with "%s"', info.response);
transporter.close();
}
module.exports = sendEmail;
调用
const SendEmail= require("./SendEmail");
//
SendEmail("123456@qq.com", 123456);