Disposable e-mail for automated tests

Create inboxes from your test suite, wait for the confirmation mail and read the code, with a REST API and webhooks.

Read the API documentation

Wait instead of sleep

The wait endpoint holds a request for up to 30 seconds and answers as soon as a message arrives, so tests need no fixed pauses.

Codes as data

Every message comes with the detected code and link, the sender, the subject, the text and sanitized HTML.

Webhooks

A signed POST reaches your URL for every new message. The content is included only if you switch it on.

Tokens with limits

Personal tokens carry only the permissions they need and only reach the inboxes of your own account.

What a test e-mail should prove

Delivery is only the start. A good end-to-end test also checks that the message is any use:

  • Every link, not just the confirmation link. The logo, the footer and "Manage preferences" often still point to production. Check the host and https of every link, in HTML and text.
  • The link works without a session. Open it in a fresh browser context. Some links only work because the test is still signed in.
  • Once means once. A second click on the same reset link must fail, and so must an expired code.
  • "Resend code". The second message carries a new code, and the old one stops working.
  • Text and HTML. Both parts should contain the code and the link. Some mail programs only show the text.
  • Readable without images. TossPost blocks remote images until you load them, as some mail programs do, so you see whether the message still works.
  • Sender and headers. From, Reply-To and the subject match the environment and the user's language. messages/{id}/raw returns the original for header checks.
  • Unintended changes. messages/{id}/previous?scope=account compares a message with the previous one from its template, across all inboxes of your account, and returns the changed lines. You notice when a deploy changed the text.

Newsletters need an unsubscribe link and a List-Unsubscribe header; a password reset does not.

Why public inboxes and SMTP mocks are not enough

Anyone who knows the name can read a public disposable inbox. Strangers see the reset links of your test accounts, and two parallel runs with the same name read each other's codes.

An SMTP catcher or mock only sees what your app hands over. It cannot tell whether the sending service's API key is valid in this environment, whether the service signs with DKIM for your domain and whether it delivers at all. A real inbox at the end of the real sending path can.

If your sign-up checks addresses against lists of disposable domains, it may reject TossPost addresses too. Then create the test inboxes on your own domain with the domain parameter of POST /mailboxes.

Keep staging and production apart

In staging, no message should reach a real person, even if the database holds real addresses. Mail traps (Settings → Mail traps) take care of that: enter a trap's SMTP login in your staging app's mail settings, and everything it sends lands in one inbox of your account, whoever it was addressed to. A trap replaces your sending service, though, so a test that should cover the service itself needs an inbox from the API.

In production you do not want test accounts. Let a scheduled job send a message to a permanent inbox at regular intervals and add a mail heartbeat there (My inboxes → Heartbeat). If the message is overdue, arrives later than the grace period or fails DKIM, TossPost tells you by push and through your webhooks.

A flow with curl

The guide How to test e-mails in automated tests covers the basic pattern with Playwright. Here it is in shell, with a resent code:

API=https://tosspost.com/api/v1
H=(-H "Authorization: Bearer $TOSSPOST_TOKEN" -H "Accept: application/json")

inbox=$(curl -s -X POST "$API/mailboxes" "${H[@]}" -d lifetime=10)
id=$(jq -r .data.id <<<"$inbox")
since=$(jq -r .data.created_at <<<"$inbox")

# the test signs up with .data.address
first=$(curl -s -G "$API/mailboxes/$id/messages/wait" "${H[@]}" --max-time 35 \
  -d timeout=30 --data-urlencode "since=$since")

# the test clicks "Resend code"
second=$(curl -s -G "$API/mailboxes/$id/messages/wait" "${H[@]}" --max-time 35 \
  -d timeout=30 -d "after=$(jq -r .data.id <<<"$first")")

curl -s -X DELETE "$API/mailboxes/$id" "${H[@]}"

--data-urlencode matters because the + in the timestamp would otherwise arrive as a space. after takes the id of the previous message and returns the next one, so two messages in quick succession are both caught. The wait endpoint returns the code, link, subject and sender; fetch text, HTML and attachments with GET /messages/{id}.

Limits that make tests fail

  • 120 requests per minute per token. Polling in a loop uses that up fast; the wait endpoint needs one request per message.
  • Two waiting requests per account. Any further one gets 429.
  • 25 active inboxes on a free account. Once the limit is reached, POST /mailboxes answers 422. Aborted runs that never cleaned up are the usual cause.
  • Hard-coded addresses. Once a message to a fixed test address hard-bounces, many sending services put the address on a suppression list and stop delivering to it. A fresh address per run avoids that.

Clean up

DELETE /mailboxes/{id} deletes the inbox and its messages, and the name is free again at once. With keep_name=1 it stays reserved, as after expiry, useful when the test account lives on in your app. A ten-minute lifetime catches runs that abort before cleaning up. Permanent inboxes for fixed test accounts keep messages for 30 days. The API documentation lists every endpoint.