What is PHPMailer and how does it work with WordPress?

PHPMailer is an open-source code that is used for sending emails from PHP applications. It is used by CMS like WordPress, Drupal, and Joomla. PHPMailer is the best PHP mail library alternative of PHP mail() function.

Examples of SMTP (Simple Mail Transfer Protocol) service providers that can be integrated with PHPMailer include Amazon SES, Gmail, Microsoft Outlook, SendGrid, Sendinblue, and Mailgun. 

How PHPMailer Works with WordPress

PHPMailer Script in WordPress
PHPMailer Script in WordPress/Photo

If you are using WordPress, you do not have to go through intricate steps of installing PHPMailer library as WordPress comes with PHPMailer class. It is available at yourwebsite.com/public_html/wp-includes/class-phpmailer.php (as show in the image above). This means you can start sending emails from your WordPress dashboard without any setting. You will be using your domain hosting SMTP server to send emails, for instance, box6176.bluehost[dot]com.

However, if you send bulk emails from a shared server; your web host may suspend your domain for exceeding web mail limits. Therefore, you need to use PHPMailer with a third-party SMTP service provider and it is simple to integrate in WordPress.  

You just need to add the code linked below to your theme’s functions.php file. Make sure that you have customized the fields marked in red (use this method with precaution)

WordPress PHPMailer SMTP setup
WordPress PHPMailer SMTP setup/Photo

Download Code from WordPress Codex.

WARNING: I do not recommend adding the code provided above to functions.php as it poses a lot of security issues. You’d better use this wp_config.php route

Update: WordPress 5.5+ now supports PHPMailer version 6.1.6.

Recommendation: For the most efficient and fastest email delivery with PHPMailer, we do recommend a2hosting PHPMailer Hosting Solution.

Features of PHPMailer  

PHPMailer can be installed on systems like Linux, Windows, and Ubuntu server. Some of the capabilities and features of PHPMailer library include:  

  • Can be used to send emails to multiple recipients with Bcc and Cc
  • Can be used to reply to emails 
  • You can attach files in your emails 
  • Supports UTF-8 
  • Email Validation  
  • DKIM (Domain Keys Identified Mail) and SPF (Sender Policy Framework) setup 
  • You can send emails without a local server 
  • SMTP authentication 
  • SMTP SSL encryption 
  • You can use it to send HTML emails (Read on best HTML Email Templates
  • Can be used for creating contact forms 

How to setup PHPMailer on cPanel  

Below is a basic PHPMailer setup guide for cPanel users not using WordPress:  

  • Go to GitHub and download PHPMailer (stable) Zip file 
  • Create an Email Address on cPanel or use your Gmail 
  • Go back and click File Manager 
  • Create a new folder in Public HTML 
  • Open the folder you have created and upload the Php Mailer Zip file that you downloaded from GitHub 
  • Extract the Zip file 
  • Create a new file and name it mail.php 
  • Open the  mail.php file and enter this code (Edit and customize the code you have just copied to include your SMTP email credentials)
Setup PHPMailer SMTP credentials
Setup PHPMailer SMTP credentials/Photo
  • Click save changes button 

NOTE: Ensure that you have set up your SPF and DKIM records so that your emails do not go to spam.  

Requirements for Installing PHPMailer for Windows  

To install PHPMailer on windows you need a website Development environment like XAMPP, EasyPHP, or WAMP and Composer. If you want to use PHPMailer without Composer, you can download PHPMailer zip file from GitHub and extract it into your preferred directory. 

Important: If you manage to properly Install PHPMailer library in your server and setup SMTP authentication, then it means that you do not need to install SMTP WordPress plugins for email deliverability.