Mastering Email Testing and Sending in PHP
Testing and sending emails are very important aspects of email deliverability. There are various standards and authentication such as SMTP and MIME, Base64 and DKIM that must be followed if you are using PHP based online-services to test and send emails.
There has been a rise in the number of alternative methods that we may use to test and send emails with PHP throughout the years, which will be discussed in this post. Let’s get started!
Table of Contents
Testing Emails in PHP
- Email Testing Checklist
- Various methods for testing emails in PHP
Sending Emails with PHP
- Built-in PHP Mail Function
- PHP Libraries
- PHP Frameworks tools
- CMS Tools
- Email Service
Testing Emails in PHP
Email testing is a very important step before sending out your emails to your subscribers. There are a variety of reasons why we should care about testing emails before sending, one of which is to ensure that the contents of your email are rendered as intended.
Email Testing Checklist
Before sending out your emails, there are a number of things to consider and a checklist to follow. The following are some of the most important things we want to test and verify:
- The email headers to make sure are created correctly.
- The text of the email is properly displayed.
- Images, links, and fonts are all accurate.
- HTML markup is correct, and relevant attachments are included.
- HTML & CSS aren’t causing issues
- Your spam score hasn’t been surpassed, and your domain hasn’t been banned.
Various Methods used for Testing Emails with PHP
The creation of a huge number of scripts and packages by developers to execute basic tests in a development environment is common practice. As a result of this procedure, the native email testing mechanism in PHP is severely restricted.
It is possible that you may need to conduct any of the following to test your emails:
- Perform all necessary tests using a variety of third-party email testing apps, such as Mailtrap.
- Create fictitious email accounts in order to simulate emailing to several recipients.
- Send and forward test emails to your own email accounts using a variety of email applications and examine them on all of the devices accessible.
Using a third-party email testing app is very efficient; for example, you can integrate Mailtrap with the PHP packages such as PHPMailer, Swift Mailer, Pear Mail, etc. to test your emails more effectively and efficiently. Here is a comprehensive list of free email testing tools that you may use to test your emails, based on what you want to test.
Sending Emails with PHP
After you’ve tested your emails, it’s time to send them out to your subscribers. Below are some of the methods to send an email using PHP, depending on your specific needs and requirements.
- Built-in PHP mail() function.
- PHP Libraries
- PHP Frameworks
- Content Management tools (CMS)
- Email Service
We’ll go through the benefits and drawbacks of each method listed below.
Built-in PHP mail() function
The built-in PHP mail() method is readily available, absolutely free, and often does not need any further configuration. When used for simple text-based alerts in a local setting, it performs well. Using the PHP mail() function is most suitable if you just need to send a few emails in a simple message format and aren’t concerned with the branding or deliverability of messages.
Using PHP’s mail function(), you can perform the following:
- Send basic text messages
- Use localhost and settings to send email
However this approach has some drawbacks to it as well:
- There is a possibility that you may send an incorrect email that will not reach the intended recipient and will wind up in spam.
- Many hosting companies do not permit the usage of a third-party SMTP server when setting up PHP mail().
- In terms of dealing with large volumes of data or frequent submissions, it is not very efficient at all.
- You do not have access to statistics on deliverability and open rates.
The following code demonstrates how to send an email using the built-in PHP mail() function.
<?php
$message = wordwrap(‘Hi, This is my first mail!’, 70, ‘\r\n’, true);
mail(‘my@example.com’, ‘Hi Friend’, $message);
PHP Libraries
PHP Mailer, Swift Mailer, and Pear Mail are some of the libraries available to make sending emails easier. PHPMailer is one of the most popular amongst them. However, for sending multiple emails Pear Mailer comes in handy. These libraries have a number of benefits which are:
- Ensuring that addresses and text are properly encoded.
- It is possible to include many formats and attachments in a single email.
- Ensuring that email headers are prepared in accordance with the standards.
- Errors are dealt with in a more timely and effective way.
Although libraries have their advantages, they also have their drawbacks, such as the following:
- It is only via commercial SMTP providers that you can have access to delivery data.
- Unsubscribing from a mailing list and keeping track of those that unsubscribe are not included as standard functionality with these libraries.
The following code demonstrates how to send email using PHPMailer:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require ‘vendor/autoload.php’;
$mail = new PHPMailer(true);
try {
$mail->setFrom(‘hello@example.net’, ‘Test Mailer’);
$mail->addAddress(‘my@example.com’, ‘Jane’);
$mail->Subject = ‘Hi Friend’;
$mail->Body = ‘Hi, This is my first mail’;
$mail->send();
} catch (PHPMailer\PHPMailer\Exception $e) {
echo “Email sending Errpr: {$mail->ErrorInfo}”;
}
For more in-depth code samples of the different libraries on this is explained in this blog post sending email using PHP.
Framework tools
It is advisable to use your framework’s built-in library rather than an external one. The most popular ways for sending email messages in PHP are through Symfony’s Mailer and Laravel’s Mailable class. They have similar features as PHPMailer but are more practical due to their close link to PHP.
Symfony Mailer supports both Sendmail and SMTP. The framework comes with pre-installed plugins for commercial email providers like SendGrid and Mailgun.
To send an email, here’s an example of Symfony Mailer code:
<?php
$email = (new Symfony\Component\Mime\Email())
from(‘hello@example.net’)
to(‘my@example.com’)
subject(‘Hi Friend’)
text(‘Hi, This is my first mail’);
/**
@var Symfony\Component\Mailer\MailerInterface $mailer
*/
$mailer->send($email);
CMS Tools: WordPress, Joomla, etc
Many well-known CMS tools such as WordPress, Joomla etc utilize PHPMailer to send emails, which is one of the key advantages of using CMS tools. If you need to add your own email-sending code, you must follow the CMS tools instructions.
However, CMS tools have the same drawbacks as PHP Libraries when it comes to sending emails.
Here is an example of code for sending email from WordPress:
<?php
wp_mail(‘my@example.com’, ‘Hi Friend’, ‘Hi, This is my first mail’);
Email Services
External services such as Sendgrid, Mailgun, Mailjet etc offer more advanced solutions for sending emails using PHP. This method can be considered if you need more SMTP authentication support and a wide variety of HTML features.
Final words
We’ve provided various methods used for sending and testing emails in PHP. Testing emails before sending them out is very important. When carrying out tests from your email testing checklists before sending out your emails using the right tools is very essential as it makes the process of testing and sending emails faster.