Test email in three layers. Local development sends to an SMTP catcher on your machine, so nothing is delivered. Automated tests mock the email API and check what your code would have sent. Staging sends through your real provider, but with its own key and subdomain, and every recipient outside an allowlist is rewritten. The goal is simple: no customer ever gets an email from a test environment.
Why email testing goes wrong
- A staging database copied from production still contains real customer addresses.
- A test sends to
test@gmail.com, which belongs to someone. - A seeding script sends a welcome email to every fake user, and they all bounce.
- Staging shares production’s API key, so a test burst counts against your production reputation and limits.
For fake data, use the domains reserved for documentation and testing by RFC 2606: example.com, example.org and the .test and .invalid top-level domains. They are safe in seed data and fixtures because they belong to nobody, but they do not accept mail, so they must never reach a real provider.
Layer 1: local development with an SMTP catcher
Run an open source catcher such as Mailpit. By default it accepts SMTP on port 1025 and shows every message in a web interface on port 8025. Nothing is delivered.
docker run -d --name mailpit -p 1025:1025 -p 8025:8025 axllent/mailpit
Point your app’s SMTP settings at localhost:1025 with no authentication or TLS when NODE_ENV=development. You can check the HTML, the text part, headers and attachments in the browser. If your app sends through an HTTP API instead of SMTP, switch transports by environment, as in the code below.
Layer 2: automated tests and CI
Unit and integration tests should not call a real email API. Intercept the HTTP request and assert on it:
- The right template and subject were chosen.
- The recipient, Reply-To and tags are correct.
- An
Idempotency-Keyis present where retries are possible. - Escaping works: a user name with
<script>arrives escaped.
Most HTTP mocking libraries can do this, or inject the send function and replace it with a fake in tests.
Layer 3: staging that sends real email, safely
Staging is where you test deliverability, rendering in real clients, and webhooks. It needs real sending, with walls around it:
| Wall | How |
|---|---|
| Separate sending domain | staging.acme.com, verified on its own |
| Separate API key | Sending only, locked to the staging domain |
| Recipient allowlist | Your team’s addresses and a few test inboxes you own |
| Rewrite everything else | Send to a shared test inbox, with the original recipient in the subject |
| Visible marker | [STAGING] subject prefix so nobody mistakes it for production |
| Scrubbed data | Replace customer emails in copied databases with addresses you own |
Example staging email
From: Acme Staging <hello@staging.acme.com>
To: qa-inbox@acme.com
Subject: [STAGING] (for sam@customer-example.com) Reset your Acme password
Hi Sam,
Reset your password: https://staging.acme.com/reset?token=...
Code: one send function, three environments
This Node.js module uses Nodemailer for local development, the Mailcandor email API elsewhere, and enforces the staging allowlist before anything leaves.
import nodemailer from 'nodemailer';
const ENV = process.env.APP_ENV; // development | staging | production
const ALLOW = new Set((process.env.EMAIL_ALLOWLIST ?? '').split(',').filter(Boolean));
const QA_INBOX = process.env.EMAIL_QA_INBOX; // an inbox you own
const local = nodemailer.createTransport({ host: 'localhost', port: 1025, secure: false });
function guard(email) {
if (ENV !== 'staging') return email;
const to = [].concat(email.to);
const blocked = to.filter((a) => !ALLOW.has(a.toLowerCase()));
return {
...email,
to: blocked.length ? QA_INBOX : to,
cc: undefined,
bcc: undefined,
subject: `[STAGING]${blocked.length ? ` (for ${to.join(', ')})` : ''} ${email.subject}`,
};
}
export async function sendEmail(email) {
if (ENV === 'development') {
await local.sendMail(email);
return { id: 'local' };
}
const res = await fetch('https://api.mailcandor.com/v1/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.MAILCANDOR_KEY}`, // a different key per environment
'Content-Type': 'application/json',
},
body: JSON.stringify(guard(email)),
});
const data = await res.json();
if (!res.ok) throw new Error(`${data.error.type}: ${data.error.message}`);
return data;
}
Keep every send in your app going through this one function. A single direct call elsewhere bypasses the guard.
Where Mailcandor fits
Mailcandor does not have test-mode keys today, so the staging walls above are yours to build. Two things help. First, until a team verifies a domain it is in a sandbox and can send only to its own team members, which is a safe place for a first integration. Second, unlimited domains are included on every plan, and sending keys can be locked to one domain, so staging.acme.com with its own key costs nothing extra and cannot send as production. Staging emails appear in the same log, so you can check headers and delivery events from webhooks as you would in production. Team roles and audit logs are covered on the security page.
Common pitfalls
- Fake addresses on real domains.
test123@gmail.commay be a real person, and invented addresses bounce. Watch your bounce rate. - Production key in staging. Staging bugs then spend production reputation and rate limit.
- Copied production data. Scrub email addresses before staging ever starts its job queue.
- Testing only in the catcher. Local tools show the HTML, not how Gmail or Outlook renders it or whether it lands in spam. Check real inboxes in staging.
- Forgetting background jobs. Cron jobs and queues send email too. Make sure they use the same guarded function.