NodeMailer NPM Tutorial

Send and receive email using NodeMailer in Node JS.

npm nodemailer email test account

NodeMailer is a popular NodeJS package for sending and receiving emails. It's open source and has no external dependencies! Let's see it in action:

NodeMailer requires access to an SMTP server, if you don't have access to one or need a frontend solution try MailSlurp's email API

Installing NodeMailer

First create a new NodeJS project with npm init -y.

Then install NodeMailer from NPM with npm install --save nodemailer.

Next setup your SMTP connection

Import nodemailer package and create a new instance.

const nodemailer = require("nodemailer");

Then connect your SMTP server:

const transporter = nodemailer.createTransport({
  host: "your-smtp-server",
  port: 587,
  secure: false,
  auth: {
    user: "smtp-user",
    pass: "smtp-password"
  }
});

Note: if you don't have access to an SMTP server use MailSlurp instead!

Sending emails

You can send emails as follows:

let info = await transporter.sendMail({
  from: "foo@example.com",
  to: "bar@example.com, baz@example.com",
  subject: "Hello",
  text: "Hello world",
});

What about receiving email?

Unfortunately NodeMailer can't easily receive emails but there are tons of email APIs out there to help.

With MailSlurp you can create test email addresses then receive emails in Javascript using them. Let's see it in action:

npm install --save mailslurp-client

Then create an inbox and wait for emails to be received.

const MailSlurp = require('mailslurp-client').default;
const mailslurp = new MailSlurp({ apiKey });
const inbox = await mailslurp.createInbox();

Use the wait for methods to receive the email

const email = await mailslurp.waitForLatestEmail(inbox.id, timeout);
// { body: "Hello world", subject: "Hello", ...rest }

Use SMTP servers with MailSlurp

/assets/dash4/2.png

Creating inboxes

You can create mailboxes in Javascript using MailSlurp.

const { id, emailAddress } = await mailslurp.createInbox();

Related content

NodeJS MailSlurp SDK

Nodemailer alternatives to send and receieve email in code and tests

Fastest way to start a typescript project

Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.

Fetch error message javascript

Handle response exceptions with Fetch in Javascript

GraphQL API disposable email accounts

How to create real email addresses using GraphQL to send and receive emails in tests and frontend applications.

GraphQL Email API Tutorial

Did you know you can send and receive emails using GraphQL?

Hugo responsive image srcsets

Serve responsive picture tags with custom render-image layout partial in Hugo static site generator.

How to send emails in Javascript (NodeJS)

JS SMTP email sending guide using HTML and NodeJS.

NodeMailer NPM Tutorial

Send and receive email using NodeMailer in Node JS.

Create custom print classes with Tailwind

Configure tailwind.config.js to create prefixed classes with a custom media query.

CypressJS Example

Test email sign-up. password verification and more with Cypress JS and MailSlurp.

CypressJS Email Testing

Use real email accounts in CypressJS to test user sign-up, email verification, and more.

Jest, Puppeteer

Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.

Webdriver, JS, WDIO

Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.

TestCafe end-to-end MFA testing for user sign-up and email verification

End-to-end testing with MailSlurp, NodeJS, and TestCafe.

Base64 file uploads

How to encode files as Base 64 encoded strings in several languages

Deno Email Apis

Send and receive emails in Deno JS. Use APIs in Deno with MailSlurp.

Email read (opened seen settings)

How to control the seen or read settings for an email using MailSlurp.

GraphQL Email API

Fetch and read emails with GraphQL inboxes using MailSlurp email API.

Testing OTP password link username and password for 2 factor authentication (2FA)

Testing OTP password link username and password for 2 factor authentication (2FA)

MailSlurp NodeMailer SMTP Usage

Use SMTP nodemailer with MailSlurp disposable email addresses

Testing email with Cypress test email accounts

Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.

Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server

Testing Email with Cypress JS and MailSlurp

Email testing with Cypress JS

Ready to dive in?Start building email applications today.