Videos
Check out our tutorial video series.
Sending and Receiving Emails in PHPUnit Tests with MailSlurp: Real emails for PHP integration tests
This post covers sending and receiving emails from test email accounts in PHP integration tests. We will use PHPUnit and the free library MailSlurp to read and write email in code without SMTP mail servers. PHPUnit is a popular PHP test library. It is used with many popular PHP frameworks such as Laravel. When used with the free MailSlurp PHP SDK you can create test email accounts then send and receive emails in code.
Many modern web applications rely on email: for user sign-up, password reset, newsletters, contact forms and more. To test these end to end you need programmable email accounts. MailSlurp is a free API that lets you create these on demand.
In PHP applications, reading emails programmatically is essential for validating workflows involving email communication. This is particularly relevant in scenarios like user registration, password resets, and two-factor authentication (2FA) processes. Automated tests that can read and verify email contents ensure that these critical features function correctly, enhancing the reliability of the application.
MailSlurp offers a convenient solution for PHP applications to receive and interact with emails without the need for direct SMTP server connections. It provides a PHP client that enables developers to easily integrate email functionalities into their applications and tests. MailSlurp works by polling email endpoints, allowing it to wait for and retrieve emails as soon as they arrive. This method is efficient for extracting OTPs or verification codes from emails, which is crucial in automated testing scenarios.
You can install the official PHP library from packagist using composer:
First create a in your project root like so:
Next run:
Next, to demonstrate how to send and receive email in PHP using MailSlurp let's write a test that create two email addresses on demand and then sends an email between them. We will also use MailSlurp to extract email contents for a make-believe confirmation code.
That is a big code block. Let us explain how it works.
The PHP code snippet above demonstrates the use of the MailSlurp Email API Client in a PHPUnit test suite for a PHP application. The code is structured as a PHPUnit test class, , which contains methods to test various email functionalities provided by MailSlurp.
private method is responsible for setting up the MailSlurp configuration. It uses an API key, which is retrieved from an environment variable
. This configuration is essential for authenticating and interacting with the MailSlurp API.Test for Creating and Receiving Emails:
method tests the ability to create a new email inbox using MailSlurp. It initializes the
object with the configuration.
, and the test asserts that the inbox's email address ends with "@mailslurp.com", verifying the creation of a valid MailSlurp email address.Test for Sending and Receiving Emails Between Two Inboxes:
method performs a more complex test. It involves creating two separate inboxes and sending an email from one inbox to the other.
, and a
instance is also initialized.
) to the second inbox (
). The email contains a specific subject ("Test") and a body with a confirmation code ("Confirmation code = abc123").
using
from
. This method waits for a specified timeout period (30 seconds in this case) for a new unread email to arrive.
, is addressed to
, has the correct subject, and contains the expected confirmation code in the body.Testing OTP verification is critical in modern web applications to ensure the security and integrity of user authentication processes. By automating OTP extraction and verification through tools like MailSlurp, developers can rigorously test the security layers of their applications. This ensures that the OTP systems work as intended, providing an extra layer of security against unauthorized access and improving the overall trustworthiness of the application.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.