If you need reliable email assertions in PHP tests, treat email as an external system with deterministic checks.
This guide shows a practical pattern:
- create test inboxes with the API
- send an email between those inboxes
- wait for delivery
- assert subject, body, sender, recipient, and extracted tokens
Why API-based inbox testing instead of local SMTP only
Local SMTP traps are useful for basic development, but API-driven inboxes are usually better for CI and integration tests because you can:
- create isolated inboxes per test run
- wait for specific messages instead of sleeping
- parse and assert dynamic content (codes, links, IDs)
- run the same flow in local, CI, and shared environments
Prerequisites
- PHP 8+
- Composer
- PHPUnit
- a MailSlurp account and API key from app.mailslurp.com
Set your API key as an environment variable:
Install dependencies
End-to-end PHPUnit example
Pattern for Laravel projects
For Laravel, keep transport and assertions separate:
- keep your app sending through your normal mail channel
- use MailSlurp in tests to observe what users would actually receive
- assert business-critical payloads (links, codes, locale strings, legal footer)
This avoids test-only mail internals leaking into production code.
Hardening checklist for CI
- create fresh inboxes per test to avoid cross-test contamination
- use explicit wait conditions instead of
calls - keep message assertions focused on business outcomes
- mask secrets and rotate API keys regularly




