Nodemailer

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Nodemailer: sending email on JavaScript (Node.js)

Nodemailer is the most popular module for sending emails from Node.js applications. It provides a simple and flexible API for working with various email services (SMTP, Gmail, Outlook, SendGrid, and others). The library supports HTML templates, attachments, streams, and secure authentication via OAuth2.

Why do you need Nodemailer?

  • Sending transactional emails (registration confirmation, password recovery).
  • Sending notifications and marketing campaigns.
  • Integration with feedback forms on the website.
  • Automatic sending of reports and logs.

Installation

Install the module via npm:

npm install nodemailer

Main features

  • Transport — configuring an SMTP server or using built-in transports (Gmail, Mailgun, SendGrid).
  • Email formatting — support for HTML, text, attachments (files, images, PDF).
  • Streams — ability to send large files without loading them into memory.
  • OAuth2 — secure authentication for Gmail and other providers.
  • Connection pool — performance optimization for mass mailings.

Code example in JavaScript (Node.js)

const nodemailer = require('nodemailer');

// Configure transport (using Gmail)
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your-email@gmail.com',
pass: 'your-password' // or use App Password
}
});

// Email parameters
const mailOptions = {
from: 'your-email@gmail.com',
to: 'recipient@example.com',
subject: 'Hello from Nodemailer!',
html: '<h1>This is a test email</h1><p>Sent via Node.js.</p>',
attachments: [{
filename: 'document.pdf',
path: './files/document.pdf'
}]
};

// Sending
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log('Error:', error);
}
console.log('Email sent:', info.response);
});

When to use Nodemailer?

The library is suitable for any Node.js project that requires sending emails: from small blogs to enterprise systems. Nodemailer integrates easily with frameworks (Express, Koa) and cloud services. If you need to send emails with attachments, HTML layout, or via OAuth2 — this is the best choice.

Please note: for production, it is recommended to use specialized services (SendGrid, Amazon SES) or SMTP relays to avoid blocks. Nodemailer supports all popular providers.

Recommendations