When implementing or updating the email sending functionality in your PHP app, you have to run numerous tests to be sure everything works as designed.Â
What and how should you test? What are the recommended options? Letâs figure it out.
Email testing approach
In the different stages of implementing the email sending functionality, you will need to test several aspects:Â
- email script (capability to send emails)
- email content (which includes multiple checks â from proper headers to the images, links, and mail merge/personalization)
- email deliverability (spam checks, in particular)
In any framework, the native email testing functionality is limited. Usually, you are able to run simple checks in a development environment. This is why developers usually create numerous scripts and packages to solve this problem. PHP is not an exception.Â
However, sooner or later, you will face the need to preview your messages and test workflows (in case of triggered emails). In any case, at some point, you will have to to do the following:Â
- send and forward test messages to your own email accounts on different clients and check them on all available devicesÂ
- use several third-party email testing apps to perform all the required checks
- generate fake email addresses to imitate sending to multiple recipients
We believe in a simpler and safer method.Â
- During development and testing, run all email checks with Mailtrap. It imitates the work of an SMTP server and catches all your email experiments to display them in its virtual inboxes. In a safe pre-production environment, you will inspect and debug your email template, test your script, mail merge and dynamic content, and you will be able to share your testing results with your team. In addition, you can automate the testing process by integrating Mailtrap via its API. Â
- Use SMTP debug and/or logging to check and fix possible sending errors.Â
Test with Mailtrap
There are a few methods or packages you can use to send emails from your PHP app. PHPMailer, Swift Mailer, and Pear Mail are among the most popular â we have inspected them in the How to Send Emails from PHP? blog post.
You can easily integrate Mailtrap with any of them, in the same way that you would do with any external SMTP server. For this purpose, specify Mailtrapâs SMTP credentials in your transport method:
Host: smtp.mailtrap.io
Port: 25 or 465 or 587 or 2525
Username: unique for each Mailtrap inbox
Password: unique for each Mailtrap inbox
TLS: Optional (STARTTLS on all ports)
To get a username and a password, go to the SMTP settings tab inside your chosen Mailtrap inbox. You will need tocreate an account if you donât have one. Registration takes less than two minutes and doesnât require a credit card as you can start for free.
PHPMailer integration sample: