Dealing with getting WordPress to properly send emails can be a source of frustration for even a seasoned developer. Relying on the web server to send email (which is what WordPress’ wp_mail() function does by default) is unreliable.
Here’s a simple method to change that – without needing a plugin. In fact, this will be more secure than 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.
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.
In fact, I’ve changed the recommended process from what I originally wrote for this article. Originally, the entire process was in a single code snippet. But I’ve had a number of more advanced users complain about security with that – and they are correct. Originally, I had focused on providing something more robust for the novice user and wanted to keep it simple. The fact is for the average novice, that would have been fine. But why not teach it better from the beginning?
So now the process is two 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.
The main code snippet then uses the constants you define in the wp_config.php file.
Setting it up
Setting up WordPress to use SMTP for sending email is extremely simple. You don’t really need any different information than you 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.
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.
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.”
This is defining your SMTP server information for phpmailer as constants that can be used later, anywhere in your WP installation. Keep in mind that 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.
The phpmailer action for wp_mail
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’s theme independent.
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.