Nodemailer for JavaScript (Node.js): Complete Guide with Examples

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

What is Nodemailer and why is it needed?

\n\n

Nodemailer is a module for Node.js that allows you to send emails from your JavaScript application. It is one of the most popular and stable libraries in the Node.js ecosystem for working with email. It provides a simple yet powerful API for creating and sending messages via various transports (SMTP, Sendmail, Amazon SES, and others).

\n\n

Why is Nodemailer needed?

\n
    \n
  • Notification automation: Sending emails about registration, password reset, order confirmation, and other events.
  • \n
  • Mass mailings: Creating newsletters, marketing campaigns (with caution to avoid spam).
  • \n
  • Feedback: Sending messages from contact forms on the website.
  • \n
  • Monitoring and logging: Notifying the administrator about critical errors in the application.
  • \n
  • Flexibility: Support for HTML templates, attachments, embedded images, and various encodings.
  • \n
\n\n

Nodemailer abstracts the complexity of working with email protocols (SMTP, POP3, IMAP) and provides a unified, clear interface.

\n\n

Installing Nodemailer

\n\n

Installing Nodemailer is done via the npm (Node Package Manager) package manager. Make sure you have Node.js installed (version 12 or higher).

\n\n

Open a terminal in the root folder of your project and run the command:

\n\n
npm install nodemailer
\n\n

After installation is complete, you can import the library into your code:

\n\n
const nodemailer = require('nodemailer');
\n\n

For projects using ES modules (with "type": "module" in package.json), use the import:

\n\n
import nodemailer from 'nodemailer';
\n\n

Quick start: sending your first email

\n\n

The simplest way to send an email is to use an SMTP server. In this example, we will use the Gmail service. Important: To work with Gmail, you will need to create an "App Password" or use OAuth2. A regular account password will not work due to Google's security settings.

\n\n
const nodemailer = require('nodemailer');\n\n// 1. Create a transport (sending method)\nconst transporter = nodemailer.createTransport({\n    service: 'gmail',\n    auth: {\n        user: 'your-email@gmail.com', // Your email\n        pass: 'your-app-password'     // App password\n    }\n});\n\n// 2. Configure the email\nconst mailOptions = {\n    from: 'your-email@gmail.com',\n    to: 'recipient@example.com',\n    subject: 'Hello from Nodemailer!',\n    text: 'This is a test email sent using Nodemailer.'\n};\n\n// 3. Send the email\ntransporter.sendMail(mailOptions, (error, info) => {\n    if (error) {\n        return console.log('Error:', error);\n    }\n    console.log('Email sent:', info.messageId);\n    console.log('Preview URL:', nodemailer.getTestMessageUrl(info));\n});\n
\n\n

This code creates a transport for Gmail, forms a simple text email, and sends it. On success, you will see the message ID in the console.

\n\n

Main methods and functions of Nodemailer

\n\n

Let's take a detailed look at the key methods provided by the library.

\n\n

1. nodemailer.createTransport(options)

\n

What it does: Creates a transport object — this is the main configuration that determines exactly how the email will be sent (through which server and with what credentials).

\n

Parameters:

\n

Recommendations