How to configure PHPMailer correctly

What is PHPMailer?
PHPMailer is one the most popular mail library. It’s used by many popular CMS platforms like WordPress and Joomla. It is one the most reliable mail library available for PHP.

Your web application development would be incomplete without the email transactions. Whether it’s a sign-up or a financial transaction you need to integrate email functionality into your web application.

The mail() function of PHP is an easy way to send emails but it relays on the sendmail library of Unix systems. Windows don’t include the local mail server so you can’t use the PHP’s mail() function.

In any such cases, PHPMailer would be an excellent alternate. It has been a popular project on Google code, Sourceforge and now fully hosted on GitHub. Have a look at the PHPMailer GitHub repository.

Using Gmail for sending email
In this article, we will be using Gmail for sending email from. You can use any email address for a receiver.

On the December 2014 onwards Google started imposing XOAUTH2 authentication mechanism based on OAuth2. If you follow any PHPMailer tutorial configured for Gmail you probably get the “Please log in via your web browser” error message in response.

To make the test application run you can turn on the less secure app to access your account. We are using it for our testing purpose only.

If you decided to use Gmail email sending for your actual production use, you should follow the PHPMailer Gmail with XOAUTH2 example. It works by creating a web app on Google Developer Console.

The setup:
Let’s implement the PHPMailer to send email using your Gmail account without wasting much of your time.

Here is the link to official PHPMailer library on GitHub. Open the page in a browser tab. We will use the composer to load the library and dependencies.

I am using Netbeans IDE for development purpose. You can use any editor/IDE of your preference.

Create a project directory and add a composer.JSON file. Add the following dependency line into the composer.JSON file.

 

Now run the composer Install command. It will find the specified library and add them to vendor directory inside your project.

Adding library using composer help you get rid of adding each required class manually. You just need to include the autoload file and create the instance of the class(es) you may need. Autoload will take care of adding classes.

If you prefer the minimal installation and don’t want to go for the composer way. You can add the class.phpmailer.PHP, class.smtp.PHP, class.pop3.PHP, and PHPMailerAutoload.php files.
Include the PHPMailerAutoload.php which will handle the loading of other classes.

The code:
Before moving ahead I assume you have created a project, added the PHPMailer library, and turn on the less secure app on your Google account.

Now let’s explore the code. I opted the composer way in my project.

 

As I said I opted for composer way. I included the autoload file and it will handle the other class file inclusion. All I need to care about is creating the instance of the class.

$mail is the instance of PHPMailer class. We will use the same object configure various mailing properties and access the function of PHPMailer to send an email.

We enabled the verbose debug output. Meaning It will display the output for each action it performs. It’s very helpful in case of failure. It will display the detailed failure message with cause. Which make debugging the code easy.

It is used to set the mailer to use SMTP. Meaning the message/mail will be transferred via SMT Protocol. You can refer the class.phpmailer.php for more information.

As we are using Gmail to send email, we need to provide the smtp endpoint of Google gmail.

For obvious reason you need to authenticate your smtp email request with valid username and password. So we set the Auth to true and provided our username and password.

Gmail work on the secure layer you can choose either SSL or TLS. We are going with SSL and Gmail listens on the port 465 for SSL connection requests.

You may need to change the Port and SMTPSecure while using the solution for your domain’s email address. You can also use SMTPSecure = FALSE and Port = 25 if your mail server doesn’t support the secure connection.

addAddress is the function where you can specify the email address of the receiver. The function takes two arguments. The first is the email address of the receiver and the second is the name of the receiver.

This will be the email address which receives the reply when the receiver hit the reply button. In some cases, the reply to address is being ignored. Place it before the setFrom method and it may solve the issue.

What you want to show as your name and email when your receiver receives an email from you. Providing your real email and name is standard practice.

The option is to set if you are sending the email as html or plain text. When you are using PHPMailer you may want to send the HTML formated emails. Keep the isHTML(true).

The option is to set the email subject.

The HTML supported mail body. Observe the tags we used in the body. You can be creative here and assigned a beautiful html email template here.

If in case the HTML format emails are not supported by the email client. We included the plain text version of mail body.

The function $mail->send() is used the send the email. The function returns the boolean value. If for some reason the email can not be sent, the function returns the error information as well.

Configuring solution for other email server:
You can use PHPMailer to send email from your branded email server. You may need to change some properties, but the overall process remains the same.

You better search for SSL/TSL SMTP ports your web hosting supports. And the hostname for your email server.

You may need to alter the Host, SMTPSecure, Port, setFrom, and addReplyTo properties to match your mail server and hosting configurations. And obviously, the username and password properties will change when you migrate from the Gmail to your email server.

If you are on CPanel based shared hosting and after making all the changes you see an error message saying “We do not authorize the use of this system to transport unsolicited 220 and/or bulk e-mail”. Here is the CPanel thread that may help you rectify the issue.

The complete code would like this:

 

Conclusion:
When you need a to send a well formatted, attachment based email from any hosting platform PHPMailer would be a good alternate. The minimalistic footprint makes the Mail an easy task. Make sure you are using the latest version of the library.

darshan

Director at AlphansoTech
Being a founder of a web application development company, Darshan finds Codeigniter 4 and WordPress very interesting. He loves to share his web development experiences with the community.

Latest posts by darshan (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *