How to intercept Nodemailer emails with MailHog

Bjorn Krolsavatar

Bjorn Krols

Published on
18 July 2021

In this article you will learn how to intercept Nodemailer emails with MailHog, a developer tool to visualize email messages.

Setting up Nodemailer

Install Nodemailer:

npm install nodemailer

If you're using TypeScript:

npm install --save-dev @types/nodemailer


import nodemailer from "nodemailer";

let mailer;
if (process.env.NODE_ENV === "development") {
  mailer = nodemailer.createTransport({
    port: 1025,
} else {
  mailer = nodemailer.createTransport({
    SES: new AWS.SES(),

Check out this article to learn how to use Nodemailer with AWS SES.

Sending an email:

await mailer.sendMail({
  from: "",
  to: "",
  subject: "Hello",
  text: "Hello, World!",

Setting up MailHog


version: "3"
    image: "mailhog/mailhog"
    container_name: "mailhog"
      - 8025:8025
      - 1025:1025

Add the following to your development script:

docker compose down
docker compose up -d

Go to http://localhost:8025/ to check the MailHog interface.

Subscribe to our newsletter

The latest news, articles, and resources, sent to your inbox weekly.

More like this