Nodejs邮件发送模块node-nodemailer的使用
Nodejs邮件发送模块node-nodemailer的使用nodemailer
是 Node.js 中一个非常流行的库,用于发送电子邮件。下面是一个简单的示例,展示如何安装和使用 nodemailer
发送邮件。
安装
首先,你需要在你的项目中安装 nodemailer
:
npm install nodemailer
基本用法
下面是一个基本的例子,演示了如何配置并使用 nodemailer
发送一封电子邮件。
const nodemailer = require('nodemailer');
// 创建一个SMTP传输对象
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'your-email@example.com', // 你的邮箱地址
pass: 'your-password' // 你的邮箱密码或者应用专用密码
}
});
// 邮件信息
let mailOptions = {
from: '"Fred Foo" <your-email@example.com>', // 发件人
to: 'bar@example.com, baz@example.com', // 收件人列表
subject: 'Hello ✔', // 邮件主题
text: 'Hello world?', // 纯文本正文
html: '<b>Hello world?</b>' // HTML正文
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// 在生产环境中,你应该检查info.response来确认邮件是否成功发送
});
使用环境变量保护敏感信息
为了安全起见,不建议直接在代码中硬编码邮箱地址和密码。可以使用环境变量来存储这些敏感信息。
首先,创建一个 .env
文件,并添加以下内容:
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
EMAIL_USER=your-email@example.com
EMAIL_PASS=your-password
然后,在你的代码中使用 dotenv
来加载这些环境变量:
npm install dotenv
require('dotenv').config();
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
secure: false,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
});
// 其余代码保持不变...
确保在 .gitignore
文件中忽略 .env
文件,以避免将敏感信息提交到版本控制系统中。
以上就是使用 nodemailer
发送电子邮件的基本步骤。你可以根据需要调整配置选项,例如添加附件、设置抄送或密送等。
当然!nodemailer
是一个非常强大的 Node.js 模块,用于发送电子邮件。首先,你需要安装它:
npm install nodemailer
接下来,你可以这样使用它来发送邮件:
const nodemailer = require('nodemailer');
// 创建一个SMTP传输对象
let transporter = nodemailer.createTransport({
host: 'smtp.yourserver.com',
port: 587,
secure: false, // 使用SSL时为true
auth: {
user: 'your-email@example.com',
pass: 'your-password'
}
});
// 邮件信息
let mailOptions = {
from: '"Fred Foo" <your-email@example.com>', // 发件人
to: 'bar@example.com, baz@example.com', // 收件人列表
subject: 'Hello ✔', // 标题
text: 'Hello world?', // 纯文本正文
html: '<b>Hello world?</b>' // HTML正文
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
});
记得替换 host
, auth.user
, auth.pass
, from
, 和 to
的值为你自己的信息。希望这能帮到你,祝你发送邮件愉快!
nodemailer
是一个流行的 Node.js 模块,用于简化电子邮件的发送。它支持多种传输方式,包括SMTP、Sendmail和Amazon SES等。以下是如何使用 nodemailer
发送电子邮件的基本步骤:
安装
首先,你需要安装 nodemailer
。可以通过 npm 来安装:
npm install nodemailer
基本用法
下面是一个简单的例子,演示如何通过 SMTP 发送邮件:
1. 导入 nodemailer
const nodemailer = require('nodemailer');
2. 创建一个运输器(Transporter)
你需要配置一个运输器对象来连接到你的邮件服务器。这通常需要你的邮箱提供商的服务器信息,比如主机名、端口、用户名和密码。
let transporter = nodemailer.createTransport({
host: 'smtp.example.com', // 你的SMTP服务器地址
port: 587, // 你的SMTP服务器端口
secure: false, // 使用TLS时为false
auth: {
user: 'your-email@example.com', // 你的邮箱账号
pass: 'your-email-password' // 你的邮箱密码或授权码
}
});
3. 发送邮件
let mailOptions = {
from: '"Fred Foo" <your-email@example.com>', // 发件人
to: 'recipient@example.com', // 收件人列表,多个收件人以逗号分隔
subject: 'Hello ✔', // 邮件主题
text: 'Hello world?', // 文本格式的邮件内容
html: '<b>Hello world?</b>' // HTML 格式的邮件内容
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// 打印邮件ID
});
注意事项
- 如果你的邮件服务器需要授权码而非密码,请确保使用授权码。
- 如果你在本地开发环境中遇到认证问题,可以考虑在你的邮件服务器上设置一个专用的应用程序密码或使用更宽松的安全设置进行测试。
- 对于生产环境,考虑使用环境变量来存储敏感信息如用户名和密码。
以上就是使用 nodemailer
发送邮件的基本流程。希望对你有所帮助!
nodemailer
是一个流行的 Node.js 模块,用于简单地发送电子邮件。首先安装它:
npm install nodemailer
然后你可以这样使用:
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'your-email@example.com', // 你的邮箱地址
pass: 'your-password' // 邮箱密码或应用专用密码
}
});
let mailOptions = {
from: '"Fred Foo" <foo@example.com>', // 发件人
to: 'bar@example.com, baz@example.com', // 收件人列表
subject: 'Hello', // 标题
text: 'Hello world?', // 纯文本正文
html: '<b>Hello world?</b>' // HTML正文
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
});
记得替换SMTP设置和邮件内容为实际值。