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
Setup:
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: "from@me.com",
to: "to@you.com",
subject: "Hello",
text: "Hello, World!",
});
Setting up MailHog
docker-compose.yml
version: "3"
services:
mailhog:
image: "mailhog/mailhog"
container_name: "mailhog"
ports:
- 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.