Here’s a simple way to make WordPress email more reliable using SMTP. These instructions will make your WP mail secure and robust.
Dealing with getting WordPress to properly send email can be a source of frustration for even a seasoned developer. Relying on the web server to send mail (which is what WordPress’ wp_mail() function does by default) is unreliable and insecure.
Here’s a simple method to change that – without needing a plugin. A plugin is a simple solution, and there are some good ones out there; but this method will give you the same benefit in a smooth and secure configuration for wp mail SMTP.
A word about security
When setting this up, you want to consider protecting your information. That’s why I don’t necessarily recommend a plugin for this.
The process of setting up wp mail SMTP is two simple code snippets. The first is adding your credentials and setting to your wp-config.php file. This keeps it away from prying eyes that might have access to your admin panel, but not your file system. This is where you make the actual wp_mail SMTP connection.
The main code snippet uses the constants you define in the wp-config.php file. Using the constants means that users without access to the config file will not have access to the actual credentials, but can still work with making the connection.
Setting up wp mail SMTP
Setting up WordPress to use SMTP for sending email is extremely simple. You don’t need any different information than you generally would for email plugin settings. All you need is your login credentials, the account information, server location and you can do this all with a few lines of simple code. (Also, your email host must allow remote connections – but that would be true if you were using this method, or a plugin method like WP Mail SMTP, which you can get learn more about here.)
Sound good? Great – let’s get started.
WordPress’s email function wp_mail is essentially a wrapper for phpmailer, a popular email class for PHP. WordPress has a little known action hook when phpmailer is initialized, phpmailer_init. This allows you to establish the phpmailer instance as using SMTP.
Setting constants in the config file
First, in your wp-config.php file, add constants for your SMTP server information. Add this somewhere in wp_config.php BEFORE the constant ABSPATH is defined. A good place is immediately before the line that has a comment saying “That’s all, stop editing! Happy blogging.”
<?php // Don't use this line. | |
/** | |
* Set the following constants in wp-config.php | |
* These should be added somewhere BEFORE the | |
* constant ABSPATH is defined. | |
* | |
* Make sure you know from your email host what your specific connection | |
* parameters are. Common defaults are given below, but if they are not | |
* what your host requires, you won't be able to connect. Each constant | |
* is noted with a comment identifying what connection parameter it is, | |
* but make sure you're using the correct value - check with your | |
* email host to confirm specific requirements. (Also confirm that your | |
* host allows remote connections in the first place.) | |
* | |
* For instructions on the use of this script, see: | |
* https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/ | |
*/ | |
define( 'SMTP_USER', 'user@example.com' ); // Username to use for SMTP authentication | |
define( 'SMTP_PASS', 'smtp password' ); // Password to use for SMTP authentication | |
define( 'SMTP_HOST', 'smtp.example.com' ); // The hostname of the mail server | |
define( 'SMTP_FROM', 'website@example.com' ); // SMTP From email address | |
define( 'SMTP_NAME', 'e.g Website Name' ); // SMTP From name | |
define( 'SMTP_PORT', '25' ); // SMTP port number - likely to be 25, 465 or 587 | |
define( 'SMTP_SECURE', 'tls' ); // Encryption system to use - ssl or tls | |
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false) | |
define( 'SMTP_DEBUG', 0 ); // for debugging purposes only set to 1 or 2 |
This is defines SMTP server information for wp mail’s phpmailer as constants that can be used later, anywhere in your WP installation.
Depending on your specific mail server settings and requirements you may need to change the values in the example. I’ve included comments to tell you what each value is. If you’re not sure, check with your email host to determine what ports, credentials, and authentication you need.
Using the phpmailer action for wp mail SMTP
Now that the constants are defined, we can set up wp_mail() to use them. This code snippet will use the SMTP constants you defined to have wp_mail() send using your SMTP server instead of your web server’s email server. You can add this to your theme’s functions.php file or build a custom plugin file to contain it. The advantage of a custom plugin file is that it is theme independent.
<?php // Don't use this line. | |
/* | |
* Add the script below to wherever you store custom code snippets | |
* in your site, whether that's your child theme's functions.php, | |
* a custom plugin file, or through a code snippet plugin. | |
*/ | |
/** | |
* This function will connect wp_mail to your authenticated | |
* SMTP server. This improves reliability of wp_mail, and | |
* avoids many potential problems. | |
* | |
* For instructions on the use of this script, see: | |
* https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/ | |
* | |
* Values for constants are set in wp-config.php | |
*/ | |
add_action( 'phpmailer_init', 'send_smtp_email' ); | |
function send_smtp_email( $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; | |
} |
Or just use a plugin
The alternative option of course is to just use a plugin. If you’ve decided to use a plugin, I recommend the aptly named WP Mail SMTP which you can learn more about here. This plugin is used on over a million sites, and I’ve been recommending it for over a decade (LONG before they had an affiliate program). Many of my WP-Members users use it for making their site’s email more robust.
Troubleshooting and more information
To use this, you will need to adjust the settings according to your email service requirements. Check with your host to make sure you know the proper port to use as well as the encryption method.
For more information on testing, troubleshooting, and changing your WordPress email configuration for wp_mail, here are some additional posts:
- Testing your WordPress email settings for the wp_mail function – some information on wp_mail and a testing script you can use to make sure it is sending messages.
- Troubleshooting wp_mail WordPress Email Configuration – not everything that can go wrong is directly a problem with WP. This post has information on host restrictions and other outside problems that should be checked.
- WordPress Email Settings: Changing the wp_mail address with a simple plugin – here is a very simple and lightweight script you can load as a plugin to change the email address that WordPress sends email from.
- Changing the wp_mail from address in WordPress without a plugin – provides a simple code snippet you can use to change the email address that WordPress sends from, no plugin required.
Enjoyed this article?
Don't miss a single post. Subscribe to our RSS feed!