Use-cases

Email testing in development and staging without emailing real users

Short answer

Test email in three layers. In local development, send to a local SMTP catcher such as Mailpit so nothing leaves your machine. In CI, mock the email API at the HTTP layer and assert on the request. In staging, send through your real provider with a separate key and sending subdomain, and rewrite every recipient that is not on an allowlist. Never send test mail to made-up addresses on real domains.

Last checked 16 November 2026

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-Key is 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.com may 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.

Questions

What is an email sandbox?

A setup where your app sends email normally but the messages are caught or restricted instead of reaching real recipients. It can be a local SMTP catcher, a provider's restricted mode, or your own recipient allowlist.

Is there a free SMTP server for testing?

Yes. Open source tools such as Mailpit run locally, accept any SMTP message and show it in a web interface without delivering it.

Can I send test emails to example.com addresses?

Not through a real provider. The example domains are reserved by RFC 2606 and do not accept mail, so the sends bounce and raise your bounce rate. Use a local catcher, or addresses you own.

Should staging use the same email domain as production?

No. Use a separate subdomain such as staging.acme.com with its own API key, so staging mistakes cannot affect production reputation and a leaked staging key cannot send as production.

Does Mailcandor have test-mode API keys?

Not at present. Until a domain is verified, a Mailcandor team can only send to its own members, and after that you control staging with a separate domain, a domain-locked key and an allowlist in your code.

Related

Send with someone on your side.

Unlimited domains on every plan, a person before any block, and 3,000 emails a month free. Tell us what you send and we set you up within one business day.