ButlerBlog

chad butler's weblog

  • About
  • Blog
  • WordPress Plugins
  • Contact
Home / Web / Blogging Tips / Easy SMTP email settings for WordPress

Easy SMTP email settings for WordPress

By Chad Butler 21 Comments

Easy SMTP email settings for WordPress

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.
  • Google
  • Facebook
  • Twitter
  • Email
  • Print
  • More
  • LinkedIn
  • Reddit
  • Tumblr
  • Pocket
  • Pinterest

Filed Under: Blogging Tips, WordPress

About Chad Butler

Chad Butler is a freelance writer and web developer. He has developed several popular WordPress plugins and has written for forbes.com, sfomag.com, and investopedia.com.

Comments

  1. Ovidiu says

    April 19, 2014 at 4:33 am

    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…

    Reply
  2. SMTP Server says

    May 22, 2014 at 3:33 am

    The information of this blog is really nice. Visit the website http://bestsmtpserver.com/

    Reply
  3. timbartrum says

    November 20, 2014 at 12:58 pm

    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!

    Reply
    • Chad Butler says

      November 24, 2014 at 10:19 am

      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).

      Reply
      • timbartrum says

        November 25, 2014 at 12:29 pm

        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.

        Reply
  4. Judah Lamm says

    January 30, 2015 at 10:40 pm

    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?

    Reply
    • Chad Butler says

      January 31, 2015 at 1:53 pm

      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.

      Reply
      • Judah Lamm says

        February 2, 2015 at 7:15 pm

        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

        Reply
  5. Kenny says

    February 17, 2015 at 3:30 pm

    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?!?

    Reply
    • jasonhendriks says

      February 26, 2015 at 7:35 am

      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

      Reply
  6. Mikolaj says

    April 2, 2015 at 8:34 pm

    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

    Reply
  7. cfascina says

    April 7, 2015 at 1:31 pm

    Great. Looked around a lot and nothing worked for me. This snippet solved all my problems. Tks!

    Reply
  8. Sam says

    October 11, 2015 at 9:56 am

    Hi,

    Thanks for this. It works like a charm. Rather use methods like these then using a plugin. Saves again on bandwidth 🙂

    Reply
  9. Mathias says

    January 13, 2016 at 11:11 am

    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!

    Reply

Trackbacks

  1. WordPress Email Configuration: Change the wp_mail "from" address without a plugin says:
    December 12, 2013 at 2:22 pm

    […] all.  It actually is pretty flexible.  You can even change your WordPress email configuration to send via an SMTP server; a method much more reliable than the generic email script.  But I’m getting ahead of […]

    Reply
  2. Testing your WordPress email settings for the wp_mail function says:
    December 13, 2013 at 9:53 am

    […] Easy SMTP email settings for WordPress – how to change your WordPress email configuration to send email from a valid SMTP server with a simple script, no plugin required. […]

    Reply
  3. Working with the WordPress email function wp_mail says:
    January 20, 2014 at 2:43 pm

    […] have put together two posts on this.  The first discusses how to handle this without a plugin.  It is not a difficult process, and I prefer this method (and use it myself) over loading YAP […]

    Reply
  4. Troubleshooting wp_mail WordPress Email Configuration says:
    November 18, 2014 at 6:14 pm

    […] address will take you a long way in this regard.  And you can take care of this either by sending through an SMTP server or just filtering the phpmailer to make this match your “from” […]

    Reply
  5. Reduce the risk of email rejection by making WordPress send emails via SMTP if DKIM is configured - Digital Scribbles says:
    December 2, 2014 at 9:37 am

    […] your theme’s functions.php to include an extra code snippet that configures WordPress to use SMTP to send […]

    Reply
  6. Change WordPress email settings to reduce spam rejection says:
    April 10, 2015 at 12:11 pm

    […] have several other posts on how to improve the reliability of email sent via wp_mail and how to troubleshoot your WordPress email settings, most of which has focused on the sending […]

    Reply
  7. Changing the WordPress email "from" address without a plugin - ButlerBlog says:
    October 17, 2016 at 9:20 am

    […] all.  It actually is very flexible.  You can even change your WordPress email configuration to send via an SMTP server; a method much more reliable than the generic email script.  But I’m getting ahead of […]

    Reply

Join in the discussion Cancel reply

Get More Content Like This

I will never share your information. No spam. No junk. No kidding. Unsubscribe anytime.

Recent Posts:

  • StudioPress Special Pricing
  • 9 Landing Page Goofs That
    Make You Lose Business [Infographic]
  • 5 reasons I use WP-Members for building membership sites
  • The Risk of Relying on Free Plugins
  • Creativity Extortion Part 2 – Turning
    Negative into Positive as a Freelancer
  • Creativity Extortion Working as a
    Freelance Designer, Developer, or Writer
  • New StudioPress Sites Offers Simplicity
    in a Powerful Hosted Package
  • The Premium Support Business Model
  • Word-based Passwords Plugin for WordPress
  • A Great Way to Get Free Gold

Archives

  • About
  • Blog
  • Archive
  • Contact

Site powered by WordPress, running on the Genesis Framework from StudioPress.

Unless otherwise noted, content on this site is © 2006-2018 ButlerBlog and may not be reproduced without express written permission from the author.

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.