Skip to main content

SMTP transport

SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so it's truly universal. Almost every email delivery provider supports SMTP-based sending, even when they advertise API‑based sending as the primary option. APIs can offer more features, but they also introduce vendor lock‑in. With SMTP you can usually swap providers by changing only the configuration object or connection URL.

Creating a transport

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport(options[, defaults]);
  • options – object that defines the connection (detailed below).
  • defaults – object merged into every message object (for example, you can set a common from address).

You can also pass a connection URL instead of an options object. Use the smtp: or smtps: protocol in the URL.

const poolConfig = "smtps://username:[email protected]/?pool=true";
const transporter = nodemailer.createTransport(poolConfig);

General options

NameTypeDefaultDescription
hoststring"localhost"Hostname or IP address of the SMTP server.
portnumber587 (465 if secure: true)Port to connect to.
securebooleanfalseIf true, the connection will use TLS immediately (recommended for port 465).
authobjectAuthentication data (see Authentication).
authMethodstring"PLAIN"Preferred authentication method.
info

Nodemailer resolves the host value with dns.resolve(). If you point host to an IP address that is not resolvable (for example, it is defined in /etc/hosts), also set tls.servername to the real hostname. TLS validation continues to work even though a DNS lookup is skipped.

TLS options

NameTypeDefaultDescription
securebooleanfalseSee General options.
tlsobjectNode.js TLSSocket options, for example { rejectUnauthorized: true }.
tls.servernamestringOptional hostname for TLS validation when host is an IP.
ignoreTLSbooleanfalseDisable STARTTLS even if the server supports it.
requireTLSbooleanfalseForce STARTTLS. If the server does not support it the message is not sent.
info

Setting secure: false does not necessarily mean you are sending in plaintext—most servers automatically upgrade to TLS via the STARTTLS command. Nodemailer follows the server’s lead unless ignoreTLS is set.

Connection options

NameDefaultDescription
namelocal hostnameHostname to use in the HELO/EHLO greeting.
localAddressLocal interface to bind for network connections.
connectionTimeout120 000 msHow long to wait for the initial TCP connect.
greetingTimeout30 000 msHow long to wait for the server greeting.
socketTimeout600 000 msIdle timeout after the greeting.
dnsTimeout30 000 msMaximum time allowed for DNS lookups.

Debug options

NameTypeDescription
loggerobject / booleanA Bunyan logger instance, true for console logging, or false / unset for no logging.
debugbooleanLog SMTP traffic when true, otherwise only transaction events.

Security options

NameTypeDescription
disableFileAccessbooleanDisallow reading content from the filesystem.
disableUrlAccessbooleanDisallow fetching content from remote URLs.

Pooling options

See Pooled SMTP for the complete list. The most important flag is:

NameTypeDescription
poolbooleanEnable connection pooling.

Proxy options

All SMTP transports support proxies. Read more in Using proxies.

Examples

1. Single connection

A new SMTP connection is created for every message:

const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: "username",
pass: "password",
},
});

2. Pooled connections

Keep a pool of connections open against an SMTP server on port 465:

const transporter = nodemailer.createTransport({
pool: true,
host: "smtp.example.com",
port: 465,
secure: true, // use TLS
auth: {
user: "username",
pass: "password",
},
});

3. Allow self‑signed certificates

Connect to a TLS server that uses a self‑signed or otherwise invalid certificate:

const transporter = nodemailer.createTransport({
host: "my.smtp.host",
port: 465,
secure: true,
auth: {
user: "username",
pass: "pass",
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
});

Authentication

If the auth object is omitted, Nodemailer treats the connection as already authenticated.

const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
});

Login

auth: {
type: "login", // default
user: "username",
pass: "password",
}

OAuth 2.0

auth: {
type: "oauth2",
user: "[email protected]",
accessToken: "generated_access_token",
expires: 1484314697598,
}

See the dedicated OAuth 2.0 guide for details, or implement a custom authentication handler if your protocol is not natively supported (see the NTLM handler for an example).

Verifying the configuration

Use transporter.verify() to make sure the SMTP configuration works.

// Promise style (Node.js 8+)
try {
await transporter.verify();
console.log("Server is ready to take our messages");
} catch (err) {
console.error("Verification failed", err);
}

// Callback style
transporter.verify((error, success) => {
if (error) {
console.error(error);
} else {
console.log("Server is ready to take our messages");
}
});

verify() checks DNS resolution, the TCP handshake, and authentication. It does not validate whether the service allows a specific envelope From address—that depends on the server configuration.

OSZAR »