Sending emails within your web applications is a necessity. Whether you're a blog with marketing and newsletter subscription needs or a full-fledged eCommerce site handling transactional data, you'll need functionality behind it to submit emails successfully to all your users.
This article walks you through setting up a PHP PEAR installation on a Ubuntu server with the ability to send HTML emails via SMTP.
What is PEAR?
Simply put, PEAR, or PHP Extension Application Repository for short, is a repository of PHP software code and libraries.
PHP prides itself on providing a solid functionality case while also maintaining its lightweight architecture. However, there are some instances where you can expand on that pre-built functionality.
In the event of sending HTML/SMTP emails with PHP, PEAR is a great solution.
Assuming you already have PHP installed on a Ubuntu instance, let's start with adding onto it with a PEAR installation.
Open up your command-line tool and enter the following command to update your server to the latest packages:
sudo apt-get update
Now that your packages are updated, let's install PEAR:
sudo apt-get install php-pear
Now, if you type
pear in your command-line tool and press the Enter key, you should see a list of available commands within PEAR. If you don't, something is wrong with your installation and you'll need to troubleshoot.
The final step for your PEAR installation is to setup PEAR to use the Mail library by entering this command:
pear install --alldeps Mail
In the rest of the article, we'll go through each of the steps to configure and submit custom emails to your website users, as well as best practices to follow when submitting emails.
1. Include the
Now we'll concentrate on creating the PHP script in your application.
First, you'll need to include the newly installed PEAR Mail library with the following line of code:
You do not need to modify the include path for this library to work. Doing so generally throws an exception. Simply including as "Mail.php" will work anywhere within your web application.
2. Set your Email Headers
Second, we'll need to define a set of headers in an array:
$headers = [
"From" => "email@example.com",
"To" => "firstname.lastname@example.org",
"Reply-To" => "email@example.com",
"Subject" => "Test Email",
"Content-Type" => "text/html"
Here, we're defining the from and to email addresses for the email submission, as well as the correct address to reply to. And we're including a custom subject line so the email subject isn't blank when users receive it.
We're also including the content type of the email. The default is plain text, so if you want to send emails containing HTML content, you'll need to change the content type to "text/html".
3. Configure your SMTP Settings
Third, we need to configure our SMTP variables by setting two arguments in the
$stmp = Mail::factory(
"host" => "my.smtp.host",
"username" => "smtp-username",
"password" => "smtp-password",
"port" => 587,
"auth" => true,
"html" => true
Here, we're assigning the
$stmp variable to our
Mail::factory() object, readying it for email submission in the next step.
The first argument is set to the value "smtp" because we'll be sending emails via an SMTP server. The second argument is an array, defining our SMTP server settings.
You'll just need to make sure that you change out your variable assignments to the correct values that match your SMTP server settings.
If you don't know your SMTP server settings, check with your hosting provider. Most hosting providers provide an SMTP server as part of their package as long as you're a customer with them.
4. Send the Email
In the last step, we'll be sending out our custom email with the library's
$body = '
<h1>This is a test email</h1>
<p>This is to test HTML within our emails.</p>
$mail = $stmp->send(
For the first argument of the
Mail::send() method, we're attaching the email address, or addresses, we'll be sending our email to. The second argument is our
$headers array we created earlier, and the final argument is our message body.
The message body can be in HTML format and you can include any styling methods you want within your emails, as long as your email styling abides by the guidelines.
Assigning a variable to your
Mail::send() method is not required, but available to view and debug email submission responses from the SMTP server.
Other Useful Tidbits
- You can send email to multiple recipients at once by changing the to value in your
$headersvariable and first argument of the
Mail::send()method to a comma-delimited list format of valid email addresses.
- You can copy other users on email submissions by including the Cc and Bcc objects within the
$headersvariable. If you want to copy multiple recipients at once, you can do so in a comma-delimited list format.
Important Note: Some hosting providers, like AWS' Simple Email Service, require that you verify your domains and email addresses before sending emails out. Check with your hosting provider to make sure that this is done properly before you start submitting emails through their servers.
Email Submission Best Practices
This isn't related to email submission functionality but should be mentioned to cover a few email etiquette points.
- Make sure you clearly describe who you are and what you're presenting to your users in your emails. Sending garbage will result in complaints that could get you permanently banned by your SMTP server provider.
- An email should only be submitted to users who are expecting it. Popular example types include when they've placed an order on your website, they should receive an order confirmation email. Or if they've signed up to your mailing list or newsletter, they would expect to receive updates from you via email on a regular basis.
- In cases where you submit emails to users who have signed up for your newsletter or mailing list, you should always provide a way to unsubscribe from the distribution lists easily. Clearly displaying a link at the bottom of your email with the title "Unsubscribe" is a good way to handle this. Make sure this link works properly.
Now, you should be able to send HTML emails to all of your users and do it correctly. There's a little bit of setup involved, but it should only take a few minutes to get up and running when sending emails with PHP PEAR.
This was the easiest implementation for my current setup. You're also able to use PHP's built-in
mail() function, I just didn't find that it had all of the bells and whistles that come with PHP PEAR Mail.