# How to test transactional email without sending to real users

A practical testing strategy for email templates, API calls, retries, and webhooks. Includes a local mock server and clear limits on what simulation proves.

Author: Stampwing
Canonical: https://www.stampwing.com/guides/test-transactional-email
Published: 2026-10-04
Reviewed: 2026-10-05
Category: Engineering

Build a repeatable email test suite that cannot accidentally contact customers.

## Short answer

Section link: https://www.stampwing.com/guides/test-transactional-email#short-answer

Test rendering, API behavior, and event handling separately using synthetic recipients and a local provider fixture. Simulated acceptance can verify application behavior, but authentication, network delivery, and inbox placement need distinct controlled checks.

## Before you start

Section link: https://www.stampwing.com/guides/test-transactional-email#before-you-start

For: Developers building local and CI email tests.

Bring: Node.js 22+, cURL and synthetic data. Keep provider credentials outside the test environment.

Scope: The local fixture checks an API contract. It cannot prove real delivery or email-client rendering.

## Choose the evidence you need from each test

Section link: https://www.stampwing.com/guides/test-transactional-email#layers

This separation makes failures explainable. If a template fixture fails, you should not need to troubleshoot DNS. If a webhook replay fails, a successful email preview is irrelevant. Avoid using a single “email test passed” badge for several unrelated claims.

| Layer | Test locally | What it does not establish |
| --- | --- | --- |
| Template rendering | Variables, escaping, links, plain text, layout | Rendering in every email client |
| API integration | Request shape, errors, stable keys, status lookup | Provider acceptance or real delivery |
| Event handling | Signature checks, duplicates, retries, ordering | That a real provider sent the fixture |
| Controlled delivery | Authentication and observations in owned test mailboxes | Inbox placement for all recipients |

## Render realistic fixtures, including awkward values

Section link: https://www.stampwing.com/guides/test-transactional-email#render

Create fixtures with long names, apostrophes, non-Latin text, missing optional fields, and very long URLs. Escape text and attribute values in the correct context. Verify the plain-text alternative alongside HTML.

Snapshot tests can catch unintended changes, but avoid treating any snapshot as automatically correct. Assert that there are no unresolved placeholders, the main action has a useful accessible name, and links use the expected scheme and host.

Use synthetic identities and sample domains. Never copy a production customer list into a test fixture, even if you believe sending is disabled. Keeping real recipient data out of the test path removes an entire class of mistakes.

| Synthetic fixture | Check |
| --- | --- |
| Name containing <, >, quotes and an ampersand | It remains text; it cannot add markup or attributes. |
| URL with a lookalike host or an unexpected scheme | The application rejects it before template rendering. |
| A missing required link or unresolved {{variable}} | Rendering fails visibly; no incomplete message is queued. |
| Long non-Latin name and long fallback URL | Essential content remains readable in both HTML and plain text. |

[Use the downloadable template fixtures](https://www.stampwing.com/templates)

## Run a local API that cannot send mail

Section link: https://www.stampwing.com/guides/test-transactional-email#mock

Download email-mock-server.mjs and run it with Node.js 22 or later. It listens only on the loopback interface, stores messages in memory, and implements a small acceptance and lookup contract. It contains no SMTP connection or provider SDK.

The fixture deliberately stays queued: it does not pretend a receiving server accepted anything. Restarting it clears its state. It is suitable for local integration exercises, not application persistence or production delivery.

| This fixture checks | Needs a different test |
| --- | --- |
| 202, stable IDs, changed-payload conflicts and lookup | Authentication, authorization and durable storage. |
| An explicitly simulated queued record | SMTP, DNS, receiving-server acceptance and inbox location. |
| A local process that can be stopped and restarted | Provider rate limits, TLS failures and ambiguous remote acceptance. |

Local fixture · two terminals

```shell
node email-mock-server.mjs

# In another terminal:
curl http://127.0.0.1:3027/api/v1/emails \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: local-receipt-42" \
  -d '{"to":"reader@example.test","subject":"Receipt","text":"A simulated receipt"}'

# Repeat unchanged: same message ID.
# Change the text with the same key: HTTP 409.
```

## Test the failure path as carefully as success

Section link: https://www.stampwing.com/guides/test-transactional-email#failure-matrix

Use a controllable clock and a seeded or injected jitter source for timing tests. Assert that no attempt runs before the persisted next-attempt time, that a restart preserves the budget, and that expired content stops the operation. Check bounds and state transitions instead of waiting through real backoff delays or expecting one random schedule.

- Validation rejection: the application shows an actionable error without recording a send as accepted.
- Lost response: the operation remains recoverable with the original key and payload.
- Rate limiting: the worker respects a delay and does not block the user request indefinitely.
- Storage unavailable: the system does not claim to have saved a message or callback.
- Duplicate callback: one event is stored once, even if the sender sees two successful responses.
- Out-of-order callback: an older observation does not erase a newer one.
- Expired content: support views handle missing retained bodies while preserving permitted event metadata.

[Run the lost-response idempotency lab](https://www.stampwing.com/guides/prevent-duplicate-emails)

[Replay signed webhook events](https://www.stampwing.com/guides/reliable-email-webhooks)

## Make each test fail when its protection is removed

Section link: https://www.stampwing.com/guides/test-transactional-email#test-the-invariant

A passing HTTP assertion can miss a duplicate side effect. Count the durable records and the transport calls as well as checking the response. Use a deliberate local mutation—such as removing the payload comparison—to check that the corresponding test can detect the bug. Do this only in a disposable test copy.

The PostgreSQL lab below exercises eight checks with no mail transport. It includes a held row lock, competing claims, a simulated lost provider acknowledgement, and a stale worker. Its reconnect check proves visibility from another database connection; it does not simulate a killed process or database failover.

| Injected fault | Assertion that matters | Insufficient assertion |
| --- | --- | --- |
| Lost response after simulated acceptance | One logical operation and one simulated provider acceptance. | The retry returned 202. |
| Changed payload under the same event key | Conflict, with the original payload unchanged. | The outbox still has one row. |
| Expired lease while outcome is unresolved | No automatic resend; stale completion updates zero rows. | Only one worker held the first lock. |
| Order transaction rolls back | Neither order nor outbox row remains. | No provider call occurred yet. |

[Run the PostgreSQL outbox lab](https://www.stampwing.com/guides/prevent-duplicate-emails#outbox-postgres-lab)

[Download outbox-lab.mjs](https://www.stampwing.com/downloads/outbox-lab.mjs)

## Put a hard boundary around automated tests

Section link: https://www.stampwing.com/guides/test-transactional-email#ci

Inject the transport into business logic rather than constructing a live provider client everywhere. Tests can supply a fixture transport, while the production entry point supplies the authenticated provider transport.

Keep live credentials out of CI jobs that exercise email behavior. Limit outbound network access where your test environment supports it. A test-only environment variable is useful, but it should not be the only barrier between a fixture and a customer mailbox.

When running a local Stampwing workspace, keep simulated mode explicit. Test keys and Live keys have different effects. The public resources do not require a Stampwing account and the downloadable fixture never sends email.

## Add a separate, deliberate delivery check

Section link: https://www.stampwing.com/guides/test-transactional-email#controlled

After local checks pass, an operator can use owned test mailboxes to observe a real message with a configured provider. Record the sender identity, time, provider events, authentication results, and observed folder. Make this a deliberate operation rather than a side effect of the test suite.

Keep the conclusion narrow. A successful message to one controlled mailbox is evidence about that message and time. It does not validate every recipient, guarantee future inbox placement, or turn simulated test results into live measurements.

[Inspect authentication with read-only DNS commands](https://www.stampwing.com/guides/spf-dkim-dmarc-for-developers)

## Define a small, repeatable test run

Section link: https://www.stampwing.com/guides/test-transactional-email#fixture-exit-criteria

1. Start the loopback fixture with no provider credentials and submit one synthetic message.
2. Repeat the identical request with the original idempotency key. Assert that the message ID is unchanged.
3. Change the payload while keeping the key. Assert a conflict rather than another accepted message.
4. Stop the fixture and verify your app reports a recoverable failure without claiming delivery.
5. Render each template with long and non-ASCII values. Check the HTML and text versions for unresolved placeholders and unexpected links.

Note: Restarting the fixture clears its memory. This run checks your integration contract, not durable storage, DNS authentication, or real delivery.

[Choose a synthetic template to render](https://www.stampwing.com/templates)

## Sources

Section link: https://www.stampwing.com/guides/test-transactional-email#sources

- [Amazon SES: interpreting delivery events](https://docs.aws.amazon.com/ses/latest/dg/event-publishing-retrieving-sns-contents.html)

Example download: [Download the local email API fixture](https://www.stampwing.com/downloads/email-mock-server.mjs)

## Related guides

- https://www.stampwing.com/guides/send-email-nextjs
- https://www.stampwing.com/guides/prevent-duplicate-emails
- https://www.stampwing.com/guides/reliable-email-webhooks

Editorial policy: https://www.stampwing.com/resources/editorial
