PHP Email Test Plugins: send and receive email in PHPUnit (example code)
How to send and receive emails in PHPUnit tests.
Sending and receiving emails from test email accounts in PHP integration tests.
About
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.
Why?
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.
Getting started
You can install the official PHP library from packagist using composer:
First create a composer.json
in your project root like so:
{
"repositories": [
{
"type": "git",
"url": "https://github.com/mailslurp/swagger-sdk-php.git"
}
],
"require": {
"mailslurp/mailslurp-client-php": "dev-master",
"phpunit/phpunit": "^8"
}
}
Next run:
composer install
Write a test using real email addresses
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.
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use PHPUnit\Framework\TestCase;
/**
* This testsuite demonstrates how to use MailSlurp Email API Client in PHP
*
* MailSlurp lets you create real email addresses in PHP. You can then send and receive emails
* and attachments in PHP applications and tests.
*
* See https://www.mailslurp.com/docs/ for more information.
*
*/
final class EmailTest extends TestCase
{
private function getConfig()
{
// create a mailslurp configuration with API_KEY environment variable
// get your own free API KEY at https://app.mailslurp.com/sign-up/
return MailSlurp\Configuration::getDefaultConfiguration()
->setApiKey('x-api-key', getenv("API_KEY"));
}
public function test_CanCreateAnInbox_ThenSendAndReceiveEmails()
{
// create an inbox controller
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->getConfig());
// create an inbox
$inbox = $inboxController->createInbox();
// verify inbox has an email address ending in @mailslurp.com
$this->assertStringContainsString(
"mailslurp.com",
$inbox->getEmailAddress()
);
}
public function test_CanSendAndReceiveEmail_BetweenTwoInboxes()
{
// create inbox and waitFor controllers
$inbox_controller = new MailSlurp\Apis\InboxControllerApi(null, $this->getConfig());
$wait_for_controller = new MailSlurp\Apis\WaitForControllerApi(null, $this->getConfig());
// create two inboxes
$inbox_1 = $inbox_controller->createInbox();
$inbox_2 = $inbox_controller->createInbox();
// send a confirmation code from inbox1 to inbox2 (sends an actual email)
$send_options = new MailSlurp\Models\SendEmailOptions();
$send_options->setTo([$inbox_2->getEmailAddress()]);
$send_options->setSubject("Test");
$send_options->setBody("Confirmation code = abc123");
$inbox_controller->sendEmail($inbox_1->getId(), $send_options);
// receive email for inbox2
$timeout_ms = 30000;
$unread_only = true;
$email = $wait_for_controller->waitForLatestEmail($inbox_2->getId(), $timeout_ms, $unread_only);
// verify emails content
$this->assertEquals($inbox_1->getEmailAddress(), $email->getFrom());
$this->assertEquals($inbox_2->getEmailAddress(), $email->getTo()[0]);
$this->assertEquals("Test", $email->getSubject());
$this->assertStringContainsString("Confirmation code = ", $email->getBody());
// extract part of an email using regex (could be used in further test steps)
$matches = array();
preg_match('/.+code = (.+)/', $email->getBody(), $matches);
$confirmation_code = $matches[1];
$this->assertEquals($confirmation_code, "abc123");
}
}
Related content
Golang email library
Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.
Send and receive email in PHP (without using mail functions)
PHP email API for creating inboxes, sending email, and receiving attachments in code and tests.
Email for testing
Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.
How to wait for Selenium to start during Codeception tests
Example tutorial for how to wait until webdriver and Selenium have started during Codeception PHP tests
Email API for email marketing and more
APIs for email marketing and social campaign testing. Send, receive, validate and test emails in code and online.
How to test an email address
Test email accounts for testing email addresses in code or online. Create fake email accounts for testing.
How to start selenium in a background process and wait for i...
Spawn Selenium server process before tests start for easier acceptance testing.
Send and receive email in Cypress JS tests with MailSlurp
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
Cypress Email Plugin - Test email accounts in Javascript (an...
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Golang mail Library (SMTP)
How to send and receive emails in Go (test email addresses).
Email APIs for Java and Kotlin projects
Test email sending and receive emails without a mail server.
Java TestNG Selenium user sign up testing with real email ad...
Testing user sign up in Java using TestNG and MailSlurp test email accounts
Codeception PHP acceptance testing using real email address ...
Write acceptance tests in PHP with real email addresses using Codeception and MailSlurp
PHP Email Test Plugins: send and receive email in PHPUnit (e...
How to send and receive emails in PHPUnit tests.
PyTest email testing
Send and receive email in Pytest Python tests.
Selenium test email plugins: send and receive email in tests...
Receive emails in Java test suites using MailSlurp, Junit, and Selenium.
Receive email in PHP: using MailSlurp to send and receive em...
Test email in PHP using real email addresses
Testing authentication using real email addresses in Ruby wi...
Cucumber example project using Capybara to test user authentication using real email addresses.
Test applications with real emails using Serenity BDD, JBeha...
Email acceptance testing with Serenity and MailSlurp. Test applications with real email addresses.
.NET SpecFlow testing using MailSlurp email accounts and Sel...
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Test email accounts with Jest and Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
Test Emails in Selenium with DotNet, CSharp and MailSlurp
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
Test emails with Cucumber and Ruby
Generate test email accounts with Ruby and Cucumber. Test email sign-up, password verification and more.
Test user sign-up with NodeJS and WebDriver
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email v...
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
How To Test Emails Before You Send
There are many free tools to test emails before sending. This can help prevent spam warnings and increase deliverability...
Testing OTP password link username and password for 2 factor...
Testing OTP password link username and password for 2 factor authentication (2FA)
Test email address
Free test email address for testing emails online with web dashboard or REST API.
Testing email-related functionality with MailSlurp
Use MailSlurp to test email related functionality using real email addresses.
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Testing Webhooks
How to test HTTP webhooks using MailSlurp test hooks.
Send SMTP emails with PHP
Use PHPMailer to send emails with SMTP and MailSlurp
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS