1. 产品
  2.   电子邮件
  3.   Node.js
  4.   Nodemailer
 
  

开源 Nodd.js API 用于电子邮件读取与发送

先进的开源 Node.js 电子邮件管理 API,使开发者能够在 Node.js 应用中创建、读取、定制并发送带有附件和自定义标题的电子邮件。

什么是 Nodemailer API?

发现 Nodemailer,这个对 Node.js 开发者来说必不可少的发送电子邮件的库。该工具使 Node.js 开发者能够仅用几行代码就在其应用程序内部创建和发送电子邮件。其开源特性和强大的 API 使其成为希望无缝集成邮件功能的开发者的热门选择。软件开发者可以在无需任何外部帮助的情况下广泛自定义邮件内容。从简单的文本邮件到带附件的复杂 HTML 模板(事务性邮件、通知或新闻通讯),该库提供了创建专业且引人入胜的邮件所需的工具。为提升安全性,它支持 OAuth2 身份验证。当从支持 OAuth2 的服务(如 Gmail)发送邮件时,此方法尤为有用。

Nodemailer 支持多种传输方式,包括 SMTP、OAuth2,甚至直接传输。这种灵活性确保开发者能够为其特定用例选择最合适的方法。它提供了广泛的自定义选项,允许开发者设置自定义头部、修改编码、添加附件、内嵌图片,并调整其他参数以满足特定需求。作为开源项目,API 受益于活跃的贡献者和用户社区。这个活跃的社区确保定期更新、持续改进,并提供丰富的共享知识和资源。Nodemailer 作为 Node.js 应用中的邮件集成解决方案,表现出强大、灵活且可靠的特性。其开源特性和强大的社区支持,使其成为开发者提升应用邮件发送能力的首选工具。

Previous Next

Nodemailer 入门指南

推荐的 Nodemailer 安装方式是使用 NPM。请使用以下命令进行顺利安装。

通过 npm 安装 Nodemailer

npm install nodemailer
 

从 Node.js 发送电子邮件

开源的 Nodemailer 电子邮件 API 允许软件开发者在 Node.js 应用程序中创建并发送电子邮件,而无需任何外部依赖。该 API 支持多种传输机制,使开发者能够选择最佳的发送邮件方式。最常见的传输方式包括 SMTP 传输、OAuth2 支持或直接传输。它支持向邮件添加附件、为 HTML 内容嵌入图像附件、向多个用户发送邮件等。下面是一个简单示例,展示软件开发者如何使用 SMTP 传输从 Node.js 应用程序发送电子邮件。

如何通过 Node.js 应用使用 SMTP 传输发送电子邮件?

 
const nodemailer = require('nodemailer');

// Configure the SMTP transport
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', // your SMTP username
        pass: 'your-email-password' // your SMTP password
    }
});

// Define the email options
let mailOptions = {
    from: 'your-email@example.com', // sender address
    to: 'recipient@example.com', // list of receivers
    subject: 'Hello from Nodemailer', // Subject line
    text: 'This is a test email sent using Nodemailer', // plain text body
    html: 'This is a test email sent using Nodemailer' // html body
};

// Send the email
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log('Error: ', error);
    }
    console.log('Message sent: %s', info.messageId);
    console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
 

从 Node.js 应用发送富内容电子邮件

Nodemailer API 使得在 Node.js 应用中创建丰富的电子邮件内容成为可能,支持纯文本和 HTML 格式。这让软件开发者能够制作视觉上吸引人的邮件,能够包含样式、图片,甚至交互式内容。通过将 API 与 Handlebars 或 EJS 等模板引擎结合使用,开发者可以创建动态、数据驱动的邮件模板。这非常适合发送个性化消息,内容可以根据用户数据进行适配。API 还支持附件,能够随邮件发送 PDF、图片或任何其他类型的文件。

自定义标题和嵌入式图像

对于高级邮件定制,Nodemailer API 允许软件开发者在 Node.js 应用中加入自定义头部和嵌入式图片。此功能对品牌化和个性化特别有用,确保邮件与应用的外观和感觉保持一致。自定义头部可用于设置邮件的特定元数据,而嵌入式图片则通过内联显示图片,使邮件在视觉上更具吸引力。下面是一个完整示例,演示如何发送带有自定义头部和嵌入式图片的邮件。

如何通过 Node.js API 发送带有自定义标题和嵌入图像的电子邮件?

 
const nodemailer = require('nodemailer');
const path = require('path');

// Create a transporter object using SMTP transport
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'your-email@gmail.com',
        pass: 'your-email-password'
    }
});

// Define the email options, including custom headers and an embedded image
let mailOptions = {
    from: 'your-email@gmail.com',
    to: 'recipient@example.com',
    subject: 'Custom Headers and Embedded Images in Nodemailer',
    html: `

欢迎!

此电子邮件包含自定义标头和嵌入的图像。

`, headers: { 'X-Custom-Header': 'CustomHeaderValue', 'X-Mailer': 'Nodemailer 6.4.11' }, attachments: [ { filename: 'image.jpg', path: path.join(__dirname, 'path_to_image/image.jpg'), cid: 'unique@nodemailer.com' // same cid as in the HTML above } ] }; // Send the email transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.error('Error sending email:', error); } console.log('Email sent successfully:', info.response); });
 中国人