Send, receive and test email with PHP
Documentation navigation
No matching docs.
Use the MailSlurp PHP client to create inboxes, send email, wait for messages in PHPUnit and read verification codes in a PHP or Laravel project.
PHP applications send email at the moments a customer is waiting for it: account confirmation, password reset, order receipts and invoices. Testing those emails needs more than PHP's mail() function or a local mail catcher. A test needs an address it controls, a wait that returns as soon as the message arrives instead of a fixed sleep(), and the subject, body, links and codes as values it can assert on. That gets harder in CI, where several runs share one environment and mail goes out through your real sending provider.
MailSlurp is an email and SMS API built for this. The mailslurp/mailslurp-client-php package creates inboxes on demand, sends email from them, waits for incoming messages and returns each email as a PHP object, so a test can check the exact message your application sent. The same inboxes work over SMTP and IMAP, and webhooks can notify your application when mail arrives.
Quick links
| Task | Start here |
|---|---|
| Install the package and create an inbox | Get started |
| Wait for an email and read a code | Receive email |
| Test a signup email in PHPUnit | Test with PHPUnit |
| Check the email a Laravel app sends | Laravel and SMTP |
| Find a PHP method and its arguments | PHP method reference |
| Find current HTTP endpoints and request fields | REST API reference |
| Find runnable projects | PHPUnit with Composer, Laravel with PHPUnit and Codeception |
| Check the released package | Packagist package |
The PHP SDK reference documents the generated client. The examples below use mailslurp/mailslurp-client-php 17.6.0 with PHP 8.3.
Get started
Create an API key
Copy an API key from the MailSlurp dashboard. Store it in your environment as MAILSLURP_API_KEY. For an organization or a support-provided demo, use the key belonging to that account.
Install the package
Add the client with Composer:
composer require mailslurp/mailslurp-client-php:^17.6
Composer installs Guzzle with it. Commit composer.lock so a later release can't change a method signature under your tests.
Configure and create an inbox
This complete script creates an inbox and prints its ID and address:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use MailSlurp\Apis\InboxControllerApi;
use MailSlurp\Configuration;
$config = Configuration::getDefaultConfiguration()
->setApiKey('x-api-key', getenv('MAILSLURP_API_KEY'));
$http = new Client(['timeout' => 60]);
$inboxes = new InboxControllerApi($http, $config);
$inbox = $inboxes->createInboxWithDefaults();
echo "Inbox ID: " . $inbox->getId() . PHP_EOL;
echo "Send your test email to: " . $inbox->getEmailAddress() . PHP_EOL;
timeout is Guzzle's HTTP time limit in seconds. Guzzle has no limit by default, so set one that is longer than any wait you ask the API for. Use the address the API returns; its domain depends on your account and inbox settings.
Email usage examples
Receive email
Send an email from the application you are testing to the address above. Set MAILSLURP_INBOX_ID to the returned inbox ID, then, after the configuration above, run:
use MailSlurp\Apis\WaitForControllerApi;
$waiting = new WaitForControllerApi($http, $config);
$email = $waiting->waitForLatestEmail(getenv('MAILSLURP_INBOX_ID'), 30000, true);
echo $email->getSubject() . PHP_EOL;
echo $email->getBody() . PHP_EOL;
preg_match('/\b\d{6}\b/', $email->getBody() ?? '', $matches);
$code = $matches[0] ?? null;
The arguments are the inbox ID, the longest time to wait in milliseconds and unread_only. The API waits up to 30 seconds and returns as soon as a new email arrives, and unread_only skips messages an earlier step already read. The regular expression reads a six-digit code from the body. To match a particular sender or subject, or to wait for several messages, see waiting for email. For confirmation links, see email verification.
Create an inbox with options
Use CreateInboxDto to name an inbox or set other options:
use MailSlurp\Models\CreateInboxDto;
$inbox = $inboxes->createInboxWithOptions(new CreateInboxDto(['name' => 'Checkout test']));
Send emails
For an account with sending enabled, send from an inbox with SendEmailOptions:
use MailSlurp\Models\SendEmailOptions;
$inboxes->sendEmail($inbox->getId(), new SendEmailOptions([
'to' => [getenv('TEST_RECIPIENT')],
'subject' => 'Order confirmation',
'body' => '<h1>Thank you for your order</h1>',
'is_html' => true,
]));
See sending and receiving email for message options and attachments for uploads and downloads.
Delete an inbox
Delete a test inbox once your assertions finish:
$inboxes->deleteInbox($inbox->getId());
For inbox expiry and cleanup across parallel test runs, see test resource lifecycle.
Test with PHPUnit
This test gives each run its own inbox, waits for the email and checks the code. Because every test has a separate address, parallel runs never read one another's mail. It runs on PHPUnit 11.
<?php
use GuzzleHttp\Client;
use MailSlurp\Apis\InboxControllerApi;
use MailSlurp\Apis\WaitForControllerApi;
use MailSlurp\Configuration;
use MailSlurp\Models\SendEmailOptions;
use PHPUnit\Framework\TestCase;
final class SignupEmailTest extends TestCase
{
private InboxControllerApi $inboxes;
private WaitForControllerApi $waiting;
private $inbox;
protected function setUp(): void
{
$config = Configuration::getDefaultConfiguration()
->setApiKey('x-api-key', getenv('MAILSLURP_API_KEY'));
$http = new Client(['timeout' => 60]);
$this->inboxes = new InboxControllerApi($http, $config);
$this->waiting = new WaitForControllerApi($http, $config);
$this->inbox = $this->inboxes->createInboxWithDefaults();
}
protected function tearDown(): void
{
$this->inboxes->deleteInbox($this->inbox->getId());
}
public function testSendsAVerificationCode(): void
{
// Replace this with the step that makes your application send its email.
$this->inboxes->sendEmail($this->inbox->getId(), new SendEmailOptions([
'to' => [$this->inbox->getEmailAddress()],
'subject' => 'Verify your account',
'body' => 'Your verification code is 482913',
]));
$email = $this->waiting->waitForLatestEmail($this->inbox->getId(), 30000, true);
$this->assertSame('Verify your account', $email->getSubject());
preg_match('/\b\d{6}\b/', $email->getBody() ?? '', $matches);
$this->assertSame('482913', $matches[0] ?? null);
}
}
As written, the test sends the email itself so you can run it straight away with vendor/bin/phpunit. In your own test, replace the sendEmail call with the action that triggers your application's email, such as submitting the signup form with $this->inbox->getEmailAddress() or calling the service that sends it. For OTP patterns and example suites, see PHPUnit email and OTP testing. If your team uses Pest, the same calls work inside a Pest test; see reading email in Pest tests.
Laravel and SMTP
In a Laravel test, Mail::fake() records mailables without sending them. That checks your mailer code, not delivery. To check the email a customer receives, run the test against an environment whose mail settings send through your real provider and use a MailSlurp inbox address as the recipient. The same waitForLatestEmail call then reads the delivered message.
If your code sends with PHPMailer, Symfony Mailer or Laravel's SMTP mailer, MailSlurp inboxes also accept SMTP and expose IMAP; see SMTP and IMAP access.
Render the email on real devices
Once MailSlurp has received the email, $email->getId() identifies it for device rendering, which captures screenshots of that exact message in Gmail, Outlook, Apple Mail and other clients. See render CI checks for waiting on a run and failing a build when a target doesn't complete.
SDK Documentation
Use the PHP method reference for published controller methods and the REST reference for current endpoints. The generated PHP reference files on GitHub list each method's arguments; select the tag that matches your installed version before copying method names. The example projects each pin the version they were written for, so check their calls against your installed version.
For message waits, OTPs, links and CI assertions in other frameworks, continue with integration testing.
Running this code needs an API key. Get one free