nodejs用nodemailer发送邮件
安装nodemailer
npm install nodemailer --save
app.js
var nodemailer = require('nodemailer');
//配置邮件
var transporter = nodemailer.createTransport('SMTP',{
host: "smtp.163.com",
secureConnection: true,
port:465,
auth: {
user: 'xxxxxx@163.com',
pass: 'xxxxxx',
}
});
//发送邮件
var sendmail = function(html){
var option = {
from:"xxxxxx@163.com",
to:"xxxxx@qq.com,xxxxxx@163.com"
}
option.subject = 'hello iVan'
option.html= html;
transporter.sendMail(option, function(error, response){
if(error){
console.log("fail: " + error);
}else{
console.log("success: " + response.message);
}
});
}
//调用发送邮件
sendmail("邮件内容:<br/>这是来自nodemailer发送的邮件!iVan");
如果是Gmail的话,配置文件这里只需要向下面一样填写即可。
//配置文件
var transporter = nodemailer.createTransport('SMTP',{
service : 'Gmail',
auth: {
user: 'xxxxxx@163.com',
pass: 'xxxxxx',
}
});
如果也想像上面一样,修改node_modeules/nodemailer/lib/wellknown.js
//省略N行
"163":{
transport: "SMTP",
host: "smtp.163.com",
port:465,
secureConnection: true,
requiresAuth: true,
domains: ["163.com"]
}
但是记得在163邮箱中开启smtp。并且上面填写的163邮箱密码,填写的是第三方授权码。不然会报550之类的错误。
下面是git地址,如果帮到了你,请赐予您那免费的star,您的star我的动力!
Git: node-sendmail