MailSlurp logo

blog

Nodemailer NPM Guide: Send Email with Node.js and Test Delivery

Install Nodemailer from npm, configure Node.js SMTP, fix common send errors, and test real inbox delivery with MailSlurp.

If you searched for npm nodemailer, nodemailer npm, node mailer, or nodemailer smtp, this guide gives you a production-safe workflow for sending email from Node.js and proving the message arrived.

Quick answer

  1. Install Nodemailer with npm install nodemailer.
  2. Configure createTransport with explicit SMTP host, port, TLS, and auth settings.
  3. Keep SMTP credentials in environment variables.
  4. Send to a MailSlurp inbox and assert the received message before release.
  5. Monitor deliverability after provider, DNS, or template changes.

Nodemailer handles message submission. MailSlurp helps prove the full workflow: SMTP handoff, real inbox receipt, headers, links, OTP codes, attachments, and CI evidence.

Nodemailer npm package quick facts

Question Answer
Package name nodemailer
Install command npm install nodemailer
Common import const nodemailer = require("nodemailer") or import nodemailer from "nodemailer"
Main API nodemailer.createTransport(...) and transport.sendMail(...)
Common default port 587 with STARTTLS, unless your provider documents another setting
Receive-side testing Use MailSlurp inbox APIs, Email Sandbox, or integration testing

Node mailer vs Nodemailer

Searches for "node mailer" usually refer to Nodemailer, the popular Node.js package for sending email over SMTP and other transports.

The package name is nodemailer. In application code, teams typically pair Nodemailer with:

  • environment-managed SMTP credentials
  • a transactional email provider or test mail server
  • receive-side tests that confirm the message actually arrived
  • deliverability checks after DNS, template, or provider changes

Install Nodemailer

npm install nodemailer

If you need to confirm the currently published package version:

npm view nodemailer version

For upgrade planning, pair this with show latest package version in npm and keep a send-and-receive smoke test around your mail path.

Minimal SMTP send example

const nodemailer = require("nodemailer");

const transport = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: Number(process.env.SMTP_PORT || 587),
  secure: false,
  auth: {
    user: process.env.SMTP_USERNAME,
    pass: process.env.SMTP_PASSWORD,
  },
});

async function sendEmail() {
  await transport.sendMail({
    from: process.env.SMTP_FROM_ADDRESS,
    to: "user@example.com",
    subject: "Welcome",
    text: "Your account is ready.",
  });
}

Before sending real traffic, verify the transporter handshake once at startup:

await transport.verify();

This catches bad host/port/auth combinations early instead of failing only on first production send.

Nodemailer createTransport settings that matter

Most production issues come from a small set of transport settings:

Setting What to check MailSlurp follow-up
host Use the provider's SMTP submission host, not a dashboard URL Validate the host with SMTP tester
port Use 587 with STARTTLS or 465 with implicit TLS based on provider docs Review which SMTP port to use
secure false commonly pairs with port 587; true commonly pairs with port 465 Confirm TLS behavior before release
auth.user and auth.pass Use environment-specific secrets or app passwords Re-test after credential rotation
from Match sender policy and domain alignment Inspect received headers with Email header analyzer

Do not treat a successful sendMail promise as the final check. It only proves Nodemailer handed the message to the next server.

Nodemailer sendMail vs MailSlurp proof

Layer Nodemailer covers MailSlurp adds
SMTP submission Transport setup and sendMail call SMTP tester diagnostics for host, port, TLS, and auth
Message generation Subject, body, HTML, attachments Received-message inspection and assertions
User workflow App triggers an email Inbox wait, link checks, OTP extraction, and timing evidence
Release control Code can send CI gates for signup, reset, invite, billing, and alert flows
Debugging SMTP errors and provider response Headers, body, attachments, webhooks, deliverability, and inbox outcome

Nodemailer plus MailSlurp receive testing

Nodemailer handles the send side of the workflow. MailSlurp completes the test by giving the message a real inbox and API assertions.

Install the packages you need for a CI smoke test:

npm install nodemailer mailslurp-client

Then send to a generated inbox and wait for the message:

const nodemailer = require("nodemailer");
const { MailSlurp } = require("mailslurp-client");

const mailslurp = new MailSlurp({
  apiKey: process.env.MAILSLURP_API_KEY,
});

const inbox = await mailslurp.createInbox();

const transport = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: Number(process.env.SMTP_PORT || 587),
  secure: false,
  auth: {
    user: process.env.SMTP_USERNAME,
    pass: process.env.SMTP_PASSWORD,
  },
});

await transport.sendMail({
  from: process.env.SMTP_FROM_ADDRESS,
  to: inbox.emailAddress,
  subject: "Nodemailer smoke test",
  text: "This email should arrive in a MailSlurp inbox.",
});

const received = await mailslurp.waitForLatestEmail(inbox.id, 30_000, true);

if (!received.subject.includes("Nodemailer smoke test")) {
  throw new Error("Expected Nodemailer message was not received");
}

This checks the real path: SMTP handoff, delivery, inbox receipt, and received-message content. Add link, OTP, attachment, and HTML assertions when the flow is customer-facing.

Test Nodemailer in CI

Use a test that fails when the user-visible workflow fails:

  1. Create a fresh MailSlurp inbox for the test run.
  2. Trigger the same Node.js code path that sends the email.
  3. Wait for the message by inbox ID.
  4. Assert sender, subject, body text, HTML, links, OTP codes, and attachments.
  5. Store the message ID and headers when a failure needs debugging.

This pattern works for signup verification, password reset, magic links, invites, receipts, and notification emails. It is stronger than checking logs because it proves the message a user would rely on was actually received.

Useful implementation paths:

Nodemailer with Gmail, Microsoft 365, and SMTP providers

Provider-specific settings often look similar but behave differently:

  • Gmail and Google Workspace may require app passwords, OAuth, or Workspace policy changes.
  • Microsoft 365 may enforce tenant SMTP AUTH rules and security defaults.
  • SMTP providers may document port 587, port 465, or a provider-specific fallback.

Before shipping provider changes, run:

  1. SMTP tester for host, port, TLS, and auth.
  2. A Nodemailer send to MailSlurp for real inbox receipt.
  3. Email header analyzer for sender and routing evidence.
  4. Email deliverability test when sender reputation matters.

Video walkthrough

This MailSlurp tutorial shows how to send email from Node.js with Nodemailer and SMTP, then think through the testing side of the workflow.

MailSlurp video Open video on YouTube

SMTP config checklist for Node teams

Validate and document:

  • host and port per environment
  • TLS mode (587 STARTTLS vs 465 TLS)
  • auth settings and credential rotation
  • sender-domain posture (SPF, DKIM, DMARC)
  • timeout and retry policy

References:

Common Nodemailer failures and fixes

EAUTH or 535 authentication errors

Likely causes:

  • invalid credentials
  • auth mechanism mismatch
  • sender-domain policy issues

Connection timeout or refusal

Likely causes:

  • wrong host/port
  • blocked outbound network access
  • TLS mismatch

SMTP accepts send but no inbox delivery

Likely causes:

  • spam-folder placement
  • reputation/authentication issues
  • recipient filtering rules

Run follow-up checks with:

Template changed and OTP parsing broke

Likely causes:

  • verification code pattern changed
  • HTML and plain-text versions drifted
  • link or token moved into a different element
  • localization changed expected content

Use MailSlurp assertions to parse the received message, not just the rendered template source.

Add receive-side assertions in CI

Do not stop at send success. Verify:

  • message arrival in expected inbox
  • subject/body/template correctness
  • OTP and verification link integrity
  • event timing and retries

Recommended workflow pages:

Production checklist for Nodemailer

  1. Keep SMTP credentials in environment variables only.
  2. Validate sender-domain auth posture before launch.
  3. Add deterministic inbox assertions for critical journeys.
  4. Capture bounce/failure signals for triage.
  5. Re-test after template, DNS, or SMTP-provider changes.

FAQ

Is Nodemailer free?

Yes, Nodemailer is open source.

Can Nodemailer receive emails?

Nodemailer is a sending library. Pair it with MailSlurp inbox APIs for receive-side testing, OTP extraction, link checks, and CI assertions.

Is Nodemailer good for production?

Yes, with robust configuration, end-to-end testing, and deliverability monitoring.

How do I test Nodemailer emails automatically?

Create a MailSlurp inbox in your test, send to that inbox with Nodemailer, wait for the email through the MailSlurp API, then assert the subject, body, links, OTP codes, headers, and attachments.

Should Nodemailer use port 587 or 465?

Use port 587 with STARTTLS for most SMTP submission. Use port 465 when your provider requires implicit TLS. Validate the chosen mode with the SMTP tester.

Next steps