
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.
Hi there, do you see a problem with having the auth user/pass stored in cleartext inside functions.php? On the other hand, if using a plugin they would be stored in the DB…
The information of this blog is really nice. Visit the website http://bestsmtpserver.com/
Screw this I’m using a plugin, hate editing the functions.php file because it all goes away as soon as you update the theme and my theme doesn’t even work with a child theme because I’ve tried it before! Do you know any plugins that do this? Also, I don’t know if my mail is the problem because what I’m trying to do is get my plugins to send emails because at the moment they’re not. For example, I’m using the plugin Send email only on Reply to My Comment and it doesn’t seem to send the email, well at least it doesn’t arrive in the inbox and it’s the same with other plugins involving email such as mailpoet. It isn’t my email account because I’ve tried various emails so it must be the WordPress configuration. Please help me, I’m so confused and frustrated! I don’t even know what SMPT means!! LOL
I’ll be so glad if you could help me, thanks!
I’d be concerned about a theme that doesn’t support child themes. That’s kind of integral to WP for quite some time.
That’s just an aside though as you certainly could use a plugin for this as well (or incorporate this code into one). I have a discussion on this blog of some troubleshooting tips and that post provides some links for various SMTP plugins.
I think that you’ll find if you get SMTP successfully implemented, you’ll have far more success with email. I don’t want to rehash things I’ve already written, but some of the issues you may be having may be the result of improperly configured parts of your email process. The above mentioned post goes into some of that as does this one.
SMTP stands for “Simple Mail Transfer Protocol.” While that’s not really important, what is important is to understand that the difference is that by default, wp_mail will send email via the phpmailer class, which is a fancy way of saying a script on the web server is handling it. Contrast that with SMTP which means that your email will be handled via an actual email server. Many hosts place restrictions on emails sent through the web server by scripts. This is to cut down on spam. Also, doing it this way is fairly unreliable as it is difficult for you to track potential problems once the email is generated, particularly if you are on a shared hosting server. Going through an SMTP server gives you more control over this, cuts down on your email being rejected (both by the sending server and the receiving server), and gives you the ability to troubleshoot emails that have been sent (but perhaps not received).
Thanks for replying 🙂 I’ve found this plugin called Mandrill and now my emails are getting through! 😀 In hotmail they go to the Junk folder though but in Gmail they go to the inbox so I’m really glad it’s working now. I think Mandrill is an SMTP server and it seems to be doing a good job at making sure the emails get through.
And also, about my theme not supporting child themes, I don’t know if it “supports them” or not but I tried creating one and then all the CSS got messed up and my website looked very weird. I’ve tried numerous times AND tried a plugin which creates a child theme for you and this gave the same results. Not sure why this happens but I’ve been putting minimal code the PHP files so I know what code to put back in if the theme updates so I’ve managed to get by without a child theme.
I want to follow up on what Ovidiu stated..
My cpanel/hosting pw is now sitting in my functions.php file.
Is there any way to hide this?
Not unless your SMTP server accepts a hash for comparison. But that leads into a number of other (rhetorical) questions to ponder (and this is in no way meant to be anything other than suggestion and discussion).
If the password for your SMTP email is the same as your cpanel/hosting password, why is that? Best practices would be to (1) not have the same password for anything, and (2) the credentials used here should be for email only – you wouldn’t want them to be the same credentials for anything else.
If your hosting company has configured things so that it’s all one account/one password, you should ask yourself, “why are you using that hosting company?” (Along that same line of thought, it’s not really a good idea to have your email and your website in the same place. Carrie Dils has a great post about this here: http://b.utler.co/f2 )
If a hacker can get to your functions.php file, they don’t need your password.
You could store all of this in your DB – and that would be more secure. Personally, I’d probably store all it as an array in a single wp_options value; retrieve it and populate the object with those values.
Hey Chad,
Thank you for the info!
Will certainly take a look into moving my webmail onto a diff platform than my hosting company.
Thanks,
Judah
Hi, just found this post in looking for a fix for a wp_mail dilemma we’ve hit. Sounds like it may be a good thing to do overall, so may give it a try.
But curious to know if this could help the specific issue we were having?
We see that you set a ‘From’ and ‘FromName’ at the end of the function. I’m assuming those become the ‘defaults’, just as ‘WordPress’ & ‘wordpress@yourdomain.com’ are for ‘wp_mail’, correct? So any default email being sent (e.g., lost password notification) would show up being from that name/email, correct?
A) I’m assuming we’d need to setup that default address on the server, even if they weren’t using hosted email here (use other domain on other server)?
B) Would this overwrite the From name/email sent from a plugin like Gravity Forms? GF typically sends the notification to wp_mail, and when I tried to add a basic function to overwrite the default From name/email it was also overwriting it for the Gravity Forms submissions, which we did NOT want. This is the main issue we’re trying to solve, wondering if you solution might help?!?
Hi Kenny, you didn’t mention what email service you are using but many will not allow you to send email from an address they do not recognize. For example Yahoo will give you connection errors and Gmail will re-write your from address. Your best bet is to sign up for domain management (e.g. Gmail Apps for Work). See https://wordpress.org/support/topic/great-plugin-5484?replies=5#post-6604953
Thanks Chad for this post, with your guidlines (including other posts about wp_mail) I was able to fix my ‘lack of email notifications’ issue.
I just copied and pasted your script that changes wp_mail to use SMTP into function.php, as you adviced, and after filling all the necessary data respectively what to my hosting provider and testing it by adding a new comment, I finnaly got a email notification.
Thanks!
Mikolaj
Great. Looked around a lot and nothing worked for me. This snippet solved all my problems. Tks!
Hi,
Thanks for this. It works like a charm. Rather use methods like these then using a plugin. Saves again on bandwidth 🙂
Hi Chad
I face problems with sending emails directly out of wordpress: no delivery to recipient. The problem is the header:
Return-Path:
Received: from something.myhoster.xy (localhost [127.0.0.1])
I use MailPoet-plugin for newsletters and Events Manager plugin for bookings. With both plugins I can use PHP Mail-method. So if I follow your guide, my e-mail headers would carry my adresses instead the one of my hoster?
I really wish I would have found your blog earlier. Thanks!