PHPMatters Help You Better Hosting Your PHP-based Sites
How to Send Email with PHPMailer

How to Send Email with PHPMailer

Recently, we have come out a tutorial concerning about how to install phpList for email management. In this post, we’d like to tell you how to use PHPMailer for email sending.

Written in 2001 by Brent R. Matzelles as a SourceForge project initially, PHPMailer now has become a popular and powerful transport class, sending emails quickly and safely from the web server using PHP codes. It supports HTML messages and emails with attachments, along with a high level of familiarity of SMTP standard protocol and few vulnerabilities for virus injection and email spamming. Also, it gives you quite a bit more control than the basic PHP mail() function.

As estimated, PHPMailer now has almost 9 million users all over the world, and the download times continue increasing significantly at a daily pace.

In the following, we have come up with a step-by-step tutorial telling you how to send email with PHPMailer instead of the built-in WordPress feature to enhance the email reliability.

Download, Unzip & Upload PHPMailer

The first thing you need to do is to download the official version of PHPMailer. You can do this by visiting https://github.com/Synchro/PHPMailer. On the webpage, you can see a Download Zip button. Just click on it.

Download PHPMailer

Now, you need to upload and extract the PHPMailer zip file to your website. You can do this with your control panel. Here, we simply assume you utilize the most popular cPanel which is supported by almost all the award-winning web hosts.

After logging into cPanel, you need to navigate to the Files section and access File Manager. Once in the manager, you can find an Upload button on the navigation bar. Click it and find the zip file. When the uploading is completed, right click on your mouse to extract the file. After refreshing the browser, you can see the PHPMailer folder in your file manager containing all its files.

If everything is right, you should notice that there is a file named README in the folder which contains the sample code for setting PHPMailer up.

PHPMailer README File

Create Two Email Accounts

Back to your cPanel and click the Email Accounts icon, you can create two email accounts that are used to send and receive the emails for testing. You can name the first account as receive_phpmailer and the second one as send_phpmailer.

Create Email Accounts

Modify the Sample Code

As is mentioned above, the README file includes the sample PHP code which is placed right below “A Simple Example”. For Now, you can start using PHPMailer by modifying the code. This is easy to be done because one of the greatest things about the example is that it includes comments helping you understand what these codes do.

PHPMailer Sample Code

So, firstly, you have to create a new file and paste the sample code in it. Then, follow the explanations below to accomplish the modification.

Initiate a PHPMailer object

require_once('AddressOfclass.phpmailer.php';)
$mail = new PHPMailer();

These lines of code means that you are creating a new PHPMailer object. Pay attention that you should fill in the correct address of the class.phpmailer.php file on your server, or there will be some errors happening.

Set up SMTP

In this step, you only need to fill in the information required according to your own condition.

$mail->IsSMTP();
$mail->Host = '{SMTP Server}';
$mail->SMTPAuth = true;
$mail->Username = '{Email Address}';
$mail->Password = '{Password}';

Email Sender and Receiver

Now, you need to configure the information about who sends the email and who will receive the email.

$mail->From = '{Sender Email Address}';
$mail->FromName = '{Your Nickname}';
$mail->AddAddress('{Receiver Email Address}', '{Receiver Nickname}');  // Add a receiver and name
$mail->AddReplyTo('{ReplyTo Email Address}', '{ReplyTo Nickname}');
$mail->AddCC('{CC Email Address}');
$mail->AddBCC('{BCC Email Address}');

Attachment

You can send any kinds of attachments as you want, so simply add the file URL between the two quotation marks. If you don’t want to send attachments, simply remove the related codes.

$mail->AddAttachment('{Attachment URI}', '{Attachment Display Name}');
$mail->IsHTML(true);

Subject and Body

Finally, you can enter a subject and the body part of your emails. The body part can be shown as the HTML so you can use a bold font, while the alternative body is in plain text for non-HTML mail clients.

$mail->IsHTML(true); // call IsHTML before setting HTML body.
$mail->Subject = 'The Subject';
$mail->Body    = 'The body message HTML';
$mail->AltBody = 'The body message in plain text';

The rest settings can be left in default. After saving all the changes, there will be a new window opened automatically carrying the words of “Message has been sent”.