Sending and receiving real emails is easy in PHP with MailSlurp: a free email as a service API.

Connecting PHP to SMTP Servers: The Conventional Method

Traditionally, PHP applications interact with SMTP servers for email functionalities. This is achieved through the use of PHP's built-in functions or libraries like PHPMailer. Here's a basic example:

This method, while functional, requires managing SMTP server configurations and handling potential deliverability issues.

Using email APIs

MailSlurp, a powerful email API, simplifies the process of sending and receiving emails in PHP. Unlike traditional SMTP methods, MailSlurp offers the flexibility of creating disposable or permanent email addresses programmatically. It eliminates the need for SMTP server setup, making it ideal for testing environments and software development. Moreover, its unique features like waiting for expected email arrivals in tests greatly reduce the complexity of email handling in PHP.

Getting started

Integrating MailSlurp into PHP projects is streamlined with Composer, a dependency manager for PHP. By adding MailSlurp's PHP library to the composer.json file, developers can easily manage the library's version and dependencies.

You can install the official PHP library from packagist using composer as follows. First create a in your project root like so:

Next run:

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.

Code explained

Let us explain the code example above.

1. Creating and Configuring a MailSlurp Client in PHP

The first part of the code sets up the MailSlurp client, which is essential for interacting with the MailSlurp API. This is achieved by including the MailSlurp configuration in the getConfig method. The configuration requires an API key, which can be obtained from the MailSlurp website. This key is essential for authenticating requests made to the MailSlurp servers. The EmailTest class extends TestCase, indicating that it is a test suite designed for PHPUnit. Within this suite, various test methods will leverage the MailSlurp client to create email inboxes and send/receive emails.

2. Testing Email Functionality: Creating Inboxes and Sending Emails

The first test method, test_CanCreateAnInbox_ThenSendAndReceiveEmails, demonstrates how to create an inbox using MailSlurp. It utilizes the InboxControllerApi to create a new inbox, and then verifies that the generated email address ends with "@mailslurp.com". This is a basic test to ensure that an inbox can be successfully created. The second test method, test_CanSendAndReceiveEmail_BetweenTwoInboxes, is more involved. It creates two separate inboxes and then sends an email from one inbox to another. This is done using the SendEmailOptions class to specify the recipient, subject, and body of the email. The method then waits for the email to be received using waitForLatestEmail, demonstrating MailSlurp's capability to send and receive real emails in a test environment.

3. Verifying Email Reception and Content Extraction

The final part of the test_CanSendAndReceiveEmail_BetweenTwoInboxes method focuses on verifying the reception of the email and extracting content from it. The test asserts that the email received matches the expected parameters such as the sender's address, recipient's address, subject, and body content. It uses regular expression matching to extract a confirmation code from the email body, showcasing how MailSlurp can be used for more advanced email interactions such as content verification and parsing. This functionality is particularly useful in scenarios where emails contain dynamic information like confirmation codes, links, or personalized data that need to be validated in automated tests.

The Advantages of Email Capabilities in PHP Applications

Incorporating email functionalities into PHP applications extends their capabilities significantly. It allows for the automation of email-based workflows, such as registration confirmations, password resets, and notifications. Using MailSlurp's API, developers can test these email-related features under real-world conditions, ensuring reliability and effectiveness. This is particularly beneficial in developing applications that rely heavily on email communications for user interactions.

Integrating MailSlurp with Laravel and Automated Testing

For Laravel applications, MailSlurp can be integrated either through Mailable SMTP settings or by calling the API directly. This flexibility enhances Laravel's already robust mailing capabilities. Furthermore, MailSlurp's disposable email accounts are invaluable in automated testing with PHPUnit and Laravel Dusk. They allow for the testing of email-related features without cluttering real email inboxes, and their throwaway nature ensures test environments remain clean and manageable.