Local SMTP catcher or real test inbox?
There are two common ways to test code that sends email. A local SMTP catcher is a small server on your machine or in a container that accepts any message your app sends and lets the test read it. A real test inbox is a service with real addresses on the internet: your app sends to it as it would to any user, and the test reads the message through an API. Neither is always better. This guide lists the trade-offs without favouring a product, says when to use each, and ends with code that lets one test suite use both by changing an environment variable.
What a local catcher is good at
- Speed. Delivery is a local network call. There is no DNS lookup, no queue at a provider and no waiting for a long poll to return.
- Offline and private. It works without internet access, and message contents never leave your machine or your CI network. That matters when emails contain data you must not send to a third party.
- No limits. There is no daily allowance, rate limit or retention rule to plan around, beyond your own disk and memory.
- Determinism. Nothing outside your control sits between the app and the test, so failures point at your code.
What a local catcher cannot tell you
- Real delivery. The app talks to a server that accepts everything. If your production provider rejects a message, a sender address is not verified or credentials are wrong, the catcher will not notice.
- DNS and authentication. Records that matter for deliverability, such as sender policy and signing, are not exercised.
- The provider's templates and links. If your provider rewrites links for click tracking or applies its own template, the catcher sees what your app sent, not what the user gets.
- Different code path. Many apps use a provider's HTTP API in production and SMTP in tests. Then the catcher tests a path that production never uses.
What a real test inbox is good at
- It tests what users get. The message crosses the same network, provider and DNS path as production mail, so links, templates and sender settings are the real ones.
- Parsed fields. A service like AssertInbox returns the message as JSON with the code and links already extracted, so the test does not parse HTML.
- Works from anywhere. Tests on a remote browser grid, a staging environment or a developer's laptop can all read the same inbox, because it is an API and not a port on one machine.
What a real test inbox costs you
- Network and latency. Tests need internet access and wait for delivery, which can take seconds.
- Limits. Every hosted service has some: a daily allowance, a retention time and a maximum message size. AssertInbox's are on the limits page.
- Data leaves your network. Only use it with test data, never with real customers' addresses or content.
- A dependency. If the service is down, tests that need it fail. Check the status page, and keep a fallback.
How it works
Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, including id, subject, otp and magic_link, or 408 if nothing arrived. Messages are deleted after 24 hours. The tag is the isolation boundary: every test builds its own tag, so tests never read each other's mail. The rest of this guide uses it for the real-inbox side.
Get an API key
Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so store it in your secret manager or CI secrets.
curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}
Export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.
When to use each
- Unit and component tests: a catcher, or no server at all if your mail library has an in-memory transport. These tests run often, so speed and independence matter most.
- Integration tests in CI that cover the sending code: a catcher, to check recipients, subject and body of what the app generates.
- End-to-end tests and staging checks: a real inbox, because these tests are meant to prove that a user can finish a flow, including receiving the mail.
- Before a release or after a provider change: a small set of real-inbox tests that send through production-like settings, to catch configuration errors that a catcher cannot see.
Using both: switch the address source
A simple way to keep one suite for both is a function that returns an address and a way to fetch its message. An environment variable chooses the implementation. Tests call getMessage and never know which one runs. This sketch assumes a catcher that exposes an HTTP API at CATCHER_URL returning a JSON list of messages for a recipient; adapt the two catcher lines to the catcher you use, since APIs differ.
const MODE = process.env.MAIL_MODE || "catcher";
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
const CATCHER_URL = process.env.CATCHER_URL || "http://localhost:8025";
let counter = 0;
function newAddress(label) {
const tag = (label + "-" + Date.now().toString(36) + "-" + ++counter).toLowerCase();
if (MODE === "inbox") return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
return { tag, email: tag + "@example.test" };
}
async function fromCatcher(address) {
for (let i = 0; i < 20; i++) {
const res = await fetch(CATCHER_URL + "/messages?to=" + encodeURIComponent(address.email));
const list = await res.json();
if (list.length) {
const body = list[0].body || "";
const otp = (body.match(/\b[0-9]{6}\b/) || [null])[0];
const link = (body.match(/https?:\/\/[^\s"<]+/) || [null])[0];
return { subject: list[0].subject, otp, magic_link: link };
}
await new Promise((resolve) => setTimeout(resolve, 500));
}
throw new Error("No message for " + address.email + " in the catcher");
}
async function fromInbox(address) {
const params = new URLSearchParams({ tag: address.tag, timeout: "25" });
for (let attempt = 0; attempt < 3; attempt++) {
const res = await fetch(API + "/v1/wait?" + params, { headers: { Authorization: "Bearer " + KEY } });
if (res.status === 200) return res.json();
if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status + " for tag " + address.tag);
}
throw new Error("No email arrived for tag " + address.tag);
}
function getMessage(address) {
return MODE === "inbox" ? fromInbox(address) : fromCatcher(address);
}
module.exports = { newAddress, getMessage };
In a unit-test job, run with MAIL_MODE=catcher (the default). In an end-to-end or staging job, run with MAIL_MODE=inbox and the two AssertInbox variables set. The catcher path above extracts a six-digit code and the first link with simple patterns; the real inbox returns otp and magic_link already. If your tests rely on edge cases of the extraction, check them in the real-inbox mode only.
A test that works in both modes
const { test } = require("node:test");
const assert = require("node:assert");
const { newAddress, getMessage } = require("./mail-source");
test("sign-up sends a verification code", async () => {
const address = newAddress("signup");
const res = await fetch((process.env.APP_URL || "http://localhost:3000") + "/api/signup", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email: address.email }),
});
assert.equal(res.status, 201);
const mail = await getMessage(address);
assert.match(mail.otp, /^[0-9]{4,8}$/);
});
A practical split
Keep most tests on the catcher and a handful on the real inbox: the sign-up, the password reset and any flow where a failed email means a lost customer. The real-inbox set runs on every merge to main or before each release, not on every commit, which keeps daily usage low. See Reliable email tests for unique tags, timeouts and cleanup.
Troubleshooting
- Real mode passes locally but fails in CI. The CI network may block outbound mail from the app, or the staging app may still be pointed at the catcher. Check the app's mail settings in that environment.
- Catcher mode passes but production mail fails. That is the gap described above. Add or keep a real-inbox test for that flow.
- Wrong address in catcher mode. The catcher may normalise recipients. Compare the address your app used with what the catcher lists.
- HTTP 429. The daily allowance is 2,000 requests per key.
Retry-Aftergives the seconds until 00:00 UTC.
Next steps
Read Reliable email tests for unique tags, retries and cleanup. Related guides: sign-up email tests in Node, GitHub Actions and password reset emails. The wait filters are described in the docs, and the client files wrap the helper for Node.js and Python.
Free beta, run by AI agents
This service is in free beta and is operated by AI agents under human supervision. Messages are deleted after 24 hours.