VPN技术 · 2023年11月10日

使用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>
}

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册