Nodejs邮件发送模块nodemailer的使用

Nodejs邮件发送模块nodemailer的使用
Nodemailer 是一个在 Node.js 中用于发送电子邮件的流行库。以下是基本的使用方法:

  1. 安装 Nodemailer:
npm install nodemailer --save
  1. 基本用法:
const nodemailer = require('nodemailer');

// 创建一个SMTP传输对象
let transporter = nodemailer.createTransport({
    host: 'smtp.example.com',
    port: 587,
    secure: false, // 使用SSL时为true,使用TLS时为false
    auth: {
        user: 'your-email@example.com', // 发件人的邮箱
        pass: 'your-password' // 邮箱密码或授权码
    }
});

// 邮件内容
let mailOptions = {
    from: '"Fred Foo 👻" <foo@blurdybloop.com>', // 发件人地址和名称
    to: 'bar@blurdybloop.com, baz@blurdybloop.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);
});
  1. 使用环境变量存储敏感信息(如邮箱和密码):
require('dotenv').config(); // 引入.env文件

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: false,
    auth: {
        user: process.env.EMAIL_USER,
        pass: process.env.EMAIL_PASS
    }
});
  1. .env 文件中定义这些变量:
SMTP_HOST=smtp.example.com
SMTP_PORT=587
EMAIL_USER=your-email@example.com
EMAIL_PASS=your-password
  1. 使用 Gmail 作为 SMTP 服务器:
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'your-email@gmail.com',
        pass: 'your-password'
    }
});
  1. 使用 OAuth2 进行身份验证(更安全):
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        type: 'OAuth2',
        user: 'your-email@gmail.com',
        clientId: 'your-client-id.apps.googleusercontent.com',
        clientSecret: 'your-client-secret',
        refreshToken: 'your-refresh-token'
    }
});
  1. 发送附件:
mailOptions.attachments = [
    {   // 二进制附件
        filename: 'text.txt',
        content: new Buffer.from('hello world!'),
        contentType: 'text/plain'
    },
    {   // URL附件
        filename: 'test.html',
        path: 'http://www.example.org/',
        cid: 'nodejsexampleid' // should be as unique as possible
    }
];
  1. 发送 HTML 模板:
const fs = require('fs');
const path = require('path');

let templatePath = path.join(__dirname, 'template.html');
let html = fs.readFileSync(templatePath, 'utf8');

mailOptions.html = html;

以上就是 Nodemailer 的基本用法。你可以根据需要调整参数以满足具体需求。


3 回复

当然!Nodemailer 是一个非常方便的 Node.js 模块,用于发送电子邮件。首先,你需要安装它:

npm install nodemailer

然后你可以这样使用它:

const nodemailer = require('nodemailer');

// 创建一个SMTP transporter对象
let transporter = nodemailer.createTransport({
    host: 'smtp.your-email-provider.com',
    port: 587,
    secure: false, // 或者 true (如果你使用的是465端口)
    auth: {
        user: 'your-email@example.com',
        pass: 'your-email-password'
    }
});

// 邮件信息
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);
});

别忘了替换SMTP服务器、邮箱地址和密码等信息。祝你发送邮件愉快!


Nodemailer 是一个非常流行的 Node.js 模块,用于发送电子邮件。它支持 HTML 邮件、附件、嵌入图片等复杂功能。下面是如何使用 Nodemailer 发送简单文本邮件和 HTML 邮件的基本步骤。

安装 Nodemailer

首先,你需要安装 nodemailer 模块。可以通过 npm 安装:

npm install nodemailer

基本使用

以下是一个基本的例子,演示如何使用 nodemailer 发送一封简单的文本邮件。

发送纯文本邮件

const nodemailer = require('nodemailer');

// 创建一个SMTP传输对象
let transporter = nodemailer.createTransport({
    host: 'smtp.example.com', // 你的SMTP服务器地址
    port: 587, // SMTP端口,通常为587或465
    secure: false, // 使用SSL时设为true
    auth: {
        user: 'your-email@example.com', // 你的邮箱账号
        pass: 'your-password' // 你的邮箱密码或授权码
    }
});

// 设置邮件内容
let mailOptions = {
    from: '"Sender Name" <sender@example.com>', // 发件人
    to: 'receiver@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);
});

注意事项

  1. 安全性:在实际应用中,不建议直接将邮箱密码硬编码在代码中。可以考虑使用环境变量或配置文件来管理敏感信息。
  2. 错误处理:确保有适当的错误处理机制,以便在邮件发送失败时能够及时发现并解决。
  3. 邮件服务提供商:不同的邮件服务提供商可能有不同的设置要求。请查阅具体提供商的帮助文档进行配置。

通过上述步骤,你可以开始使用 nodemailer 来发送电子邮件了。希望这对你有所帮助!

Nodemailer 是一个用于 Node.js 应用程序的模块,可以轻松地发送电子邮件。首先安装 nodemailer:npm install nodemailer。然后你可以这样使用:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.example.com',
    port: 587,
    secure: false, // 使用 SSL/TLS
    auth: {
        user: 'your-email@example.com',
        pass: 'your-password'
    }
});

let mailOptions = {
    from: '"Fred Foo" <foo@example.com>',
    to: 'bar@example.com',
    subject: 'Hello',
    text: 'Hello world',
    html: '<b>Hello world</b>'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) return console.log(error);
    console.log('Message sent: %s', info.messageId);
});

请替换SMTP服务器信息和认证凭据。

回到顶部