本文中使用的是QQ邮箱
打开QQ邮箱进行设置,并保存更改,获取到授权码,作为第三方登录中的密码使用。
代码:
config.js
exports.config = {
email: {
service: 'QQ',
auth: {
user: '***',
//授权码
pass: '***'
}
}
}
sendmail.js
var nodemailer = require('nodemailer')
var smtpTransport = require('nodemailer-smtp-transport');
var emailconfig = require('./config.js').config.email;
var transporter = nodemailer.createTransport(smtpTransport(emailconfig));
exports.sendMail = function (recept, subject, content) {
transporter.sendMail({
from: emailconfig.auth.user,
to: recept,
subject: subject,
html: content
}, function (error, response) {
if (error) {
console.log(error);
}
console.log('success')
});
}
main.js
var sendMails = require('./sendmail.js').sendMail;
sendMails('123@qq.com','test', 'test');