Join us
@sofiatarhonska ă» Oct 12,2022 ă» 5 min read ă» 1706 views ă» Originally posted on mailtrap.io
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.
In the different stages of implementing the email sending functionality, you will need to test several aspects:Â
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:Â
We believe in a simpler and safer method.Â
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 to create 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:
Swift Mailer integration sample:
Pear Mail integration sample:
Alternatively, you can integrate your app with Mailtrap via its API. For details, please see full Mailtrap API documentation here.Â
What happens next? As soon as you have integrated Mailtrap, you can run your email script with peace of mind. With any email addresses in To, Cc, and Bcc, all your messages will go to your Mailtrap inbox only.Â
Thatâs great, but what should you do if your message didnât arrive in the Mailtrap inbox?Â
Make sure you have enabled exceptions and configured debugging.Â
With PHPMailer, you can use the standard exceptions method. Here is a code sample of a simple HTML message:
If any error is found, you will get the description in response.Â
Also, enable SMTP debugging using one of the levelsÂ
Level 3 and 4 are used when you canât connect.Â
Level 0 turns the debugging off.Â
For example, letâs enter an invalid hostname. As a result, we will get the following messages:
2020-05-12 14:51:32 Connection: opening to mailtrap.io:2525, timeout=10, options=array()
2020--5-12 14:51:42 Connection failed. Error #2: stream_socket_client(): unable to connect to mailtrap.io:2525 (Operation timed out) [/Users/xxxx/Downloads/PHPMailer/src/SMTP.php line 326]
2020-05-12 14:51:42 SMTP ERROR: Failed to connect to server: Operation timed out (60)
2020-05-12 14:51:42 SMTP connect() failed.
Mailer Error: SMTP connect() failed.
To enable debugging in Pear Mail, setÂ
(it is false by default). The full message should look like this:Â
Swift Mailer provides the Logger plugin.Â
The code is taken from https://swiftmailer.symfony.com/docs/plugins.html
Hopefully, you have fixed your script and now your test messages are successfully delivered right to the Mailtrap inboxes.Â
Now you can safely test your email system and message content to make sure that both triggered and mass emails work exactly as expected.
Send your newly designed email template to your Mailtrap inbox. Inspect the following:
Youâve just made basic email checks. Hereâs what you can do with the advanced functionality:
+any_text
that provides you with an almost limitless number of test emails. With this feature you can imitate sending messages to multiple different recipients.Once you have verified that every piece of your email sending functionality works exactly as designed, you can switch to production.Â
With Mailtrap, you can also monitor your production system: put the address of your inbox(-es) to Bcc and instantly receive all messages sent to your users.Â
The functionality of PHP email libraries isnât designed for proper email testing. You should use specialized tools to thoroughly test every aspect of your email workflows. Mailtrap is designed for safe email testing in development and staging environments, and is compatible with any PHP framework (and other frameworks too). It allows you to test email functionality, inspect and debug your templates, run automated tests, and check complex flows.Â
That's it! I hope you enjoyed reading our guide on testing email format in PHP that was originally published on Mailtrap Blog by Diana Lepilkina.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.