Short answer
You check emails and SMS in automated tests through a tool that captures messages on the test environment and exposes them through an API: for mail, e.g. Mailpit (free, run next to the app), Mailtrap or MailSlurp; for SMS, a fake gateway or the provider’s test mode. The test triggers an action, fetches the latest message to a given address or number and checks the content, link or code. Leave real inboxes and phones for a few final tests, because they’re slow and flaky.
How it works
| step | SMS | |
|---|---|---|
| 1. staging setup | the SMTP server points to Mailpit or Mailtrap | the SMS gateway is replaced by a fake or test mode |
| 2. action in the test | sign-up, password reset, order | login with a code, phone number confirmation |
| 3. fetch the message | the tool’s API, filtered by address | the fake’s API, filtered by number |
| 4. check | content, link, code | content, code |
Each test uses a unique address or number, e.g. [email protected], so parallel tests don’t read each other’s messages.
Example in Playwright with Mailpit
Password reset: the test requests a link, fetches the email through the Mailpit API and opens the link.
import { test, expect } from "@playwright/test";
const MAILPIT = process.env.MAILPIT_URL; // e.g. http://mailpit:8025
test("password reset through the emailed link", async ({ page, request }) => {
const email = `qa-${Date.now()}@example.com`;
// ... create an account for this address through the API
await page.goto("/reset-password");
await page.getByLabel("Email").fill(email);
await page.getByRole("button", { name: "Send link" }).click();
await expect.poll(async () => {
const res = await request.get(`${MAILPIT}/api/v1/search?query=to:${email}`);
return (await res.json()).messages.length;
}).toBeGreaterThan(0);
const search = await (await request.get(`${MAILPIT}/api/v1/search?query=to:${email}`)).json();
const msg = await (await request.get(`${MAILPIT}/api/v1/message/${search.messages[0].ID}`)).json();
const link = msg.Text.match(/https?:\/\/\S+reset\S+/)[0];
await page.goto(link);
await expect(page.getByLabel("New password")).toBeVisible();
});
expect.poll waits until the email arrives, instead of a fixed pause. Check the exact API paths in the documentation for the Mailpit version you use.
What to check in every message
- recipient and subject;
- data: amounts, order number, name, date, currency;
- links: they point to the right environment and work, also when tapped on a phone;
- codes: valid for a set time, invalid once used;
- non-ASCII characters in the subject and body;
- the plain-text version of the email, not just HTML;
- no duplicates: one order, one email;
- SMS: length, because non-GSM characters such as Polish letters or emoji shorten a single message from 160 to 70 characters.
How emails look in different clients (Outlook, Gmail, Apple Mail) is checked separately, once per template change, e.g. in the Mailtrap preview or an email preview tool.
Common mistakes
- tests reading a real Gmail inbox: slow, with spam filters and 2FA;
- a fixed “wait 10 seconds for the email” pause instead of waiting for a condition;
- one shared address for all tests;
- a staging environment that emails real customers because it has a copy of the production database.
How to set up staging with mail capture is covered in how to prepare a test environment. I discuss SMS codes for two-factor login in 2FA login in automated tests.
How I can help
As part of test automation I set up mail and SMS capture on the test environment and add E2E tests for sign-up, password reset and notifications. Describe your app in the contact form.