WordPress SMTP: the easy way

Wordpress and SMTP

SMTP on WordPress without a third-party plugin

Getting WordPress to send email using SMTP is not standard with the default installation and typically requires the use of the WP Mail SMTP plugin from the WordPress Plugin Repository. In this quick tutorial, I want to show you a workaround that will allow you to use almost any SMTP service (some additional steps are required for GMail).

Determine Implementation Method

The first step is to determine if you want to implement this as part of your [child] theme or using a customized Must Use Plugin (mu-plugin). When I implement this, I typically create a mu-plugin file so that IF the theme is switched, the code is will still remain operational.

The Code

<?php
/**
 * SMTP Mailer Configuration
 *
 * @package           SMTP Mailer
 * @author            Charles Pelkey
 * @copyright         2020 C.P. Web Designs
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       SMTP Mailer
 * Plugin URI:        https://www.cp-webdesigns.com/
 * Description:       This plugin configures WP to use GMAIL or other defined SMTP service over SSL/TLS
 * Version:           1.5
 * Requires at least: 5.0
 * Requires PHP:      7.1
 * Author:            Charles Pelkey
 * Author URI:        https://www.cp-webdesigns.com
 * Text Domain:       cpwd-smtp-mailer
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 */
defined( 'ABSPATH' ) or die();
// SMTP email settings
define( 'SMTP_AUTH',	true ); // Use SMTP authentication (true|false)
define( 'SMTP_FROM',	'name@domain.com' ); // SMTP From email address
define( 'SMTP_HOST',	'smtp.service.com' ); // The hostname of the mail server
define( 'SMTP_NAME',    'FROM NAME' ); //  Business From Name
define( 'SMTP_PASSWD',	'smtp_passwd' ); // Password to use for SMTP authentication
define( 'SMTP_PORT',	'465' ); // SMTP port number - likely to be 25, 465 or 587
define( 'SMTP_SECURE',	'ssl' ); // Encryption system to use - ssl or tls
define( 'SMTP_USER',	'user@domain.com' );	// Username to use for SMTP authentication
// Initialize the Mailer Settings
function smtp_mailer_setup( $phpmailer ) {
    if ( ( ! defined( 'SMTP_HOST' ) || empty( SMTP_HOST ) ) || ( ! defined( 'SMTP_USER' ) || empty( SMTP_USER ) ) || ( ! defined( 'SMTP_PASSWD' ) || empty( SMTP_PASSWD ) ) ) return;
    
    $phpmailer->isSMTP();     
    $phpmailer->Host = SMTP_HOST;  
    $phpmailer->SMTPAuth = SMTP_AUTH;
    $phpmailer->Port = SMTP_PORT;
    $phpmailer->Username = SMTP_USER;
    $phpmailer->Password = SMTP_PASSWD;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From = SMTP_FROM;
    $phpmailer->FromName = SMTP_NAME;
}
add_action( 'phpmailer_init', 'smtp_mailer_setup' );
// Log Mailer Errors
function smtp_mailer_error( $wp_error ) {
    $mail_log = dirname( __FILE__, 2 ) . '/mailer-errors.log';
    error_log( 'Mailing Error (' . date( 'Y-m-d H:i:s' ) . '): ' . print_r( $wp_error, true ) . PHP_EOL, 3, $mail_log );
}
add_action( 'wp_mail_failed', 'smtp_mailer_error', 10, 1 );

Code Explanation and Discussion

In the code above, lines 28 through 35 setup CONSTANTS that will be used for connecting to your SMTP service and they should be pretty self-explanatory; however, I will cover them below just so there is no confusion:

  • SMTP_AUTH -> Enable authentication with the SMTP Server
  • SMTP_FROM -> This is the FROM email address that will be used when the email is sent
  • SMTP_HOST -> hostname or IP Address of the SMTP server
  • SMTP_NAME -> This is how the FROM NAME will be displayed in the email when sent
  • SMTP_USER and SMTP_PASSWD -> the username and password combination for authenticating with the server
  • SMTP_PORT and SMTP_SECURE -> port 465 and SSL are standard, alternatively you can use 587 and TLS (most services do not allow port 25 anymore due to security concerns)

Line 40 checks to make sure that SMTP_HOST, SMTP_USER and SMTP_PASSWD are defined and not empty, if they are than the function will not finish executing. This line can be expanded to include other required data (such as SMTP_PORT and SMTP_SECURE)

Lines 42 through 50 completes the setup with the phpmailer class that comes with WordPress.

The smtp_mailer_errors function (starting on line 55) appends any errors that phpmailer encounters to the file defined in the parent directory (in this examples it would create the file within the wp-content folder). You could hard-code a path (if desired) but if you ever migrate hosts, then the path will more than likely not work.

Implement the code

First, if you chose to add this to your [child] theme, I recommend placing lines 28 through 35 in the wp-config.php file, but you can simply place them at the top of your functions.php file if you prefer.

Second, copy the two functions and their corresponding add_action lines into your functions.php file.

To implement as a must use plugin [preferred method], check your WordPress wp-content folder for a sub-folder called ‘mu-plugins’… if this does not exist, create it…. second create a file name smtp-mailer.php and copy the enire contents from the code block (above) into that file and be sure to enter your SMTP settings where they belong.

Bonus: GMAIL

Are you using GMAIL? Check to see if you have the option of enabling “less secure apps” [Google Workspace or Google Cloud Identity customers can still use this as of May 30th, 2022]. If you qualify to use this option, enable it for the account you want to use with this plugin file….

if “less secure apps” is not an option for your account, check to see if you can create an “Application Password”. If you can, then set an application password and use that as the SMTP_PASSWD constant.

Read More from google on less secure apps and application passwords

Once one of these two options are enabled, enter ‘smtp.gmail.com’ as the SMTP_HOST and use ‘465’ and ‘ssl’ as the SMTP_PORT and SMTP_SECURE respectively.


If you found this article helpful, we hope you will share it and provide feedback using the comments form below…

Leave a Reply

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