VPN技术 · 2023年11月10日 0

使用Node.js发送电子邮件

Node.js发送邮件

Nodemailer模块发送邮件

nodemailer模块可以快速简单的发送邮件

安装模块

C:UsersYour Name>npm install nodemailer

比如从谷歌账号发送邮件

var nodemailer = require(nodemailer);
var transporter = nodemailer.createTransport({
service: gmail,
auth: {
user: [email protected],
pass: yourpassword
}
});
var mailOptions = {
from: [email protected],
to: [email protected],
subject: Sending Email using Node.js,
text: That was easy!
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log(Email sent:  + info.response);
}
});

多接收者

var nodemailer = require(nodemailer);
var transporter = nodemailer.createTransport({
service: gmail,
auth: {
user: [email protected],
pass: yourpassword
}
});
var mailOptions = {
from: [email protected],
to: [email protected],
subject: Sending Email using Node.js,
text: That was easy!
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log(Email sent:  + info.response);
}
});

发送HTML文件邮件,可以使用HTML属性

var mailOptions = {
from: youremail@gmail.com,
to: [email protected],
subject: Sending Email using Node.js,
html: <h1>Welcome</h1><p>That was easy!</p>
}