
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 to 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

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 shown in the image above). This means you can start sending emails from your WordPress dashboard without any settings. 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 webmail limits. Therefore, you need to use PHPMailer with a third-party SMTP service provider and it is simple to integrate in WordPress.
Functions.php option
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).

Download Code from WordPress Codex.
WARNING: I do not recommend directly adding the code provided above to functions.php as it poses security issues. If someone manages to gain access to your theme files, then your SMTP login credentials can be stolen and used to send spam emails. You’d better use the wp-config.php route as provided below.
WP Config Option
Adding SMPT authentication details to your wp-config file is more secure than using the functions.php file. Step 1 below involves initializing connection to PHPmailer while step 2 involves adding your SMTP authentication details to the wp-config file
Step 1: Add the code below to your theme’s functions.php file
// PHPMailer functions.php details
add_action( 'phpmailer_init', 'my_smtp_phpemailer' );
function my_smtp_phpemailer( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
Step 2: Add the code below to the wp-config.php file.
/** PHPMailer wp-config.php details */
define( 'SMTP_USER', 'user@website.com' ); // your SMTP Username
define( 'SMTP_PASS', 'my smtp password' ); // your SMTP Password
define( 'SMTP_HOST', 'smtp.gmail.com' ); // mail server hostname
define( 'SMTP_FROM', 'john@website.com' ); // from email address
define( 'SMTP_NAME', 'Website Name' ); // Website Name
define( 'SMTP_PORT', '25' ); // use SMTP port number - 25, 465 or 587
define( 'SMTP_SECURE', 'tls' ); // for Encryption use - ssl or tls
define( 'SMTP_AUTH', true ); // to enable SMTP authentication use (true or false)
define( 'SMTP_DEBUG', 0 );
Replace the credentials in italics above with your SMTP authentication details. The wp-config.php file is available in the public_html folder.
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 using a hosting provider that does not block outbound emails.
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)

- Click the 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 the PHPMailer zip file from GitHub and extract it into your preferred directory.
Important: If you manage to properly Install the 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.
Got questions on blogging tools, this post, or anything else related to website technologies, we are responding on Reddit or Blogiestools Ask Forum