1. Products
  2.   Email
  3.   Node.js
  4.   Nodemailer
 
  

Opne Source Nodd.js API for Emails Reading & Sending

An Advanced Open Source Node.js Email Management API enables Developers to Create, Read, Customize and Send Email Messages with Attachments from Node.js Apps.

Discover Nodemailer, the essential email-sending library for Node.js developers. The tool empower Node.js developers, to create and send email messages with just a couple of lines of code from within their applications. Its open source nature and robust API make it a popular choice for developers looking to integrate email functionality seamlessly. Software developers can customize email content extensively without any external help. From simple text emails to complex HTML templates (transactional emails, notifications, or newsletters) with attachments, the library provides the tools needed to create professional and engaging emails. For enhanced security, it supports OAuth2 authentication. This method is particularly useful when sending emails from services that support OAuth2, such as Gmail.

Nodemailer supports a variety of transport methods, including SMTP, OAuth2, and even direct transport. This flexibility ensures that developers can choose the most appropriate method for their specific use case. It provides extensive customization options and allows developers to set custom headers, modify encoding, add attachments, inline images, and adjust other parameters to meet specific requirements. As an open source project, the API benefits from a vibrant community of contributors and users. This active community ensures regular updates, continuous improvement, and a wealth of shared knowledge and resources. Nodemailer stands out as a powerful, flexible, and reliable solution for email integration in Node.js applications. Its open-source nature and strong community support make it a go-to tool for developers aiming to enhance their applications with robust email-sending capabilities.

Previous Next

Getting Started with Nodemailer

The recommend way to install Nodemailer is using NPM. Please use the following command for a smooth installation.

Install Nodemailer via npm

npm install nodemailer
 

Sending Email Message from Node.js

The open source Nodemailer email API allows software developers to create and send email messaging from Node.js application without any external dependencies. The API supports various transport mechanisms, allowing developers to choose the best method for sending emails. The most common transport methods include SMTP Transport, OAuth2 Support or Direct Transport. It supports adding attachments to emails, embedding image attachments for HTML content, sending email to multiple users and so on. Here is a simple example that shows how software developers can use SMTP transport to send an email message from Node.js application.

How to Send Email Message via SMTP Transport from Node.js Apps?

 
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));
});
 

Send Emails with Rich Content from Node.js Apps

The Nodemailer API enables the creation of rich email content, supporting both plain text and HTML formats inside Node.js applications. This allows software developers to craft visually appealing emails that can include styling, images, and even interactive content. By using the API with templating engines like Handlebars or EJS, developers can create dynamic, data-driven email templates. This is perfect for sending personalized messages, where the content can adapt based on user data. The API supports attachments, making it possible to send files such as PDFs, images, or any other type of file along with the email.

Custom Headers and Embedded Images

For advanced email customization, the Nodemailer API allows software developers to include custom headers and embedded images inside Node.js applications. This feature is particularly useful for branding and personalization, ensuring emails align with the application’s look and feel. Custom headers can be used to set specific metadata for the email, while embedded images can make your email visually appealing by displaying images inline. Below is a comprehensive example that demonstrates sending an email with custom headers and an embedded image.

How to Send an Email with Custom Header and an Embedded Image via 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: `

Welcome!

This email contains custom headers and an embedded image.

`, 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); });