Skip to main content

Usage

This page shows how to get Nodemailer up and running quickly, then walks through the most common tasks you’ll perform: creating a transporter and sending a message.

Installation

Add Nodemailer to your project:

npm install nodemailer

Create a transporter

Every email you send goes through a transporter—an object that knows how to deliver messages to your chosen email service.

const nodemailer = require("nodemailer");

// Create a transporter for SMTP
const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});

createTransport(transport[, defaults]) returns a reusable transporter instance.

ParameterType / Description
transport**Object  String  Plugin**. Either a configuration object (like the SMTP example above), a connection URL ("smtp://user:pass@smtp.example.com:587"), or a pre‑configured transport plugin instance.
defaultsObject (optional). Values that will be merged into every message you send with this transporter.
Keep the transporter

Create the transporter once and reuse it. Transporter creation opens network sockets and performs authentication; doing this for every email adds needless overhead.

Other transport types

Verify the connection (optional)

Before you start sending, you can check that Nodemailer can connect to your SMTP server:

await transporter.verify();
console.log("Server is ready to take our messages");

Send a message

Once you have a transporter, send an email with transporter.sendMail(message[, callback]).

(async () => {
try {
const info = await transporter.sendMail({
from: '"Example Team" <team@example.com>', // sender address
to: "alice@example.com, bob@example.com", // list of receivers
subject: "Hello", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});

console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
} catch (err) {
console.error("Error while sending mail", err);
}
})();

Parameters

ParameterDescription
messageEmail content and headers. See Message configuration for all options.
callback(optional) (err, info) => {}. If omitted, sendMail returns a Promise.

The info object returned by most transports contains:

PropertyDescription
messageIdThe final Message‑ID value assigned to the email.
envelopeObject with the SMTP envelope (FROM, TO).
acceptedArray of addresses accepted by the server.
rejectedArray of addresses rejected by the server.
pendingWith the direct transport: addresses that received a temporary failure.
responseThe last response string received from the SMTP server.
Partial success

If a message has multiple recipients it is considered sent as long as at least one address was accepted.