nodemailer 是一个简单易用的 Node.js 邮件发送组件,Github 项目地址为https://github.com/andris9/Nodemailer
bilibili video: link
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.163.com',
secureConnection: true,
port: 465,
secure: true,
auth: {
user: 'xxx_some@163.com',
pass: 'MQQVMSWLNHNXUPAT'
}
});
const mailOptions = {
from: 'xxx_some@163.com',
to: '632862998@qq.com',
subject: 'subject',
// text: 'haha',
html: `<b style="color: red">haha</b>`
};
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
} else {
console.log('邮件发送:' + info.response);
}
})
设置邮件选项
- from:指定发件人地址或名称。可以是字符串格式(如 “sender@example.com”),也可以是对象格式(如 { name: “Sender Name”, address: “sender@example.com” })
transporter.sendMail({
from: 'sender@example.com'
});
transporter.sendMail({
from: { name: "Sender Name", address: "sender@example.com" }
});
- to:指定收件人地址或名称,可以是字符串或数组格式。与 from 属性类似,也支持对象格式
transporter.sendMail({
to: 'recipient@example.com'
});
transporter.sendMail({
to: ['recipient1@example.com', 'recipient2@example.com']
});
transporter.sendMail({
to: [
{ name: "Recipient 1", address: "recipient1@example.com" },
{ name: "Recipient 2", address: "recipient2@example.com" }
]
});
- cc:指定抄送收件人,格式与 to 属性相同
transporter.sendMail({
to: 'primary@example.com',
cc: 'cc@example.com'
});
- bcc:指定密送收件人,格式与 to 属性相同
transporter.sendMail({
to: 'primary@example.com',
bcc: 'bcc@example.com'
});
- subject:指定邮件主题
transporter.sendMail({
subject: 'Hello from Nodemailer'
});
- text:指定纯文本格式的邮件内容
transporter.sendMail({
text: 'This is the body of the email in plain text.'
});
- html:指定 HTML 格式的邮件内容
transporter.sendMail({
html: '<h1>This is the body of the email in HTML</h1>'
});
- attachments:添加附件,可以是文件路径、URL 链接或 base64 编码数据
transporter.sendMail({
attachments: [
{ filename: 'file.pdf', path: './path/to/file.pdf' },
{ filename: 'image.png', url: 'https://example.com/image.png' },
{
filename: 'banana.png',
content: 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
encoding: 'base64'
}
]
});
- encoding:指定邮件内容的编码格式,默认为 ‘utf-8’
transporter.sendMail({
text: 'This is the body of the email in plain text.',
encoding: 'iso-8859-1'
});
- priority:设置邮件优先级,可选值为 ‘low’、‘normal’(默认)或 ‘high’
transporter.sendMail({
priority: 'high'
});
- headers:添加自定义的邮件头信息
transporter.sendMail({
headers: {
'X-Laziness-level': 1000
}
});
几种 nodemailer 使用技巧
- 发送 HTML 格式邮件
- 使用 html 属性设置 HTML 格式的邮件内容
- 支持富文本、图片和超链接等内容
transporter.sendMail({
from: 'sender@example.com',
to: 'recipient@example.com',
subject: '这是一封 HTML 格式的邮件',
html: '<h1>这是标题</h1><p>这是正文内容</p><img src="cid:unique@nodemailer.com" />'
});
- 发送附件
- 使用 attachments 属性添加附件
- 支持本地文件、URL 链接和 base64 编码数据
transporter.sendMail({
from: 'sender@example.com',
to: 'recipient@example.com',
subject: '这是一封带附件的邮件',
text: '邮件内容',
attachments: [
{
filename: 'file.pdf',
path: './path/to/file.pdf'
},
{
filename: 'image.png',
cid: 'unique@nodemailer.com',
contentType: 'image/png',
content: fs.createReadStream('./path/to/image.png')
}
]
});
- 发送多重邮件: 使用 sendMail() 方法可以一次发送多封邮件,通过循环或者 Promise.all() 的方式来实现
const mailOptions = [
{
from: 'sender@example.com',
to: 'recipient1@example.com',
subject: '第一封邮件',
text: '这是第一封邮件的内容'
},
{
from: 'sender@example.com',
to: 'recipient2@example.com',
subject: '第二封邮件',
text: '这是第二封邮件的内容'
}
];
Promise.all(mailOptions.map(option => transporter.sendMail(option)))
.then(info => console.log('所有邮件发送成功'))
.catch(error => console.error('邮件发送失败:', error));
- 设置邮件模板: 将邮件内容抽离到独立的模板文件中,可以提高邮件内容的可维护性和可重用性。可以使用 Handlebars、Pug 或者 ejs 等模板引擎来渲染模板
const hbs = require('nodemailer-express-handlebars');
transporter.use('compile', hbs({
viewPath: 'path/to/email-templates',
extName: '.hbs'
}));
transporter.sendMail({
from: 'sender@example.com',
to: 'recipient@example.com',
subject: '这是一封模板邮件',
template: 'email-template',
context: {
username: 'John Doe',
message: '这是模板邮件的内容'
}
});
- 发送邮件预览: 通过设置 preview: true 可以在发送邮件前预览邮件内容,这对于调试和测试非常有帮助
transporter.sendMail({
from: 'sender@example.com',
to: 'recipient@example.com',
subject: '这是一封预览邮件',
text: '这是邮件内容',
preview: true
});