Changing the wp_mail from address in WordPress without a plugin

One common question I often hear regarding WordPress email configuration is, “How do I change the email address from [email protected]?”  I suspect that most people are unaware that WordPress has its own function for sending email and that it has a default address that it sends from.  If they are aware, most users opt for changing this with a plugin.

WordPress has no ability to manage email settings via its admin panel, but that is not to say that you cannot manage the email configuration at 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 myself.

If your sole aim is to change the email “from” address to something that is not the default value, this is far to simple for using a plugin (unless you have complicated needs and want more control over email configuration in the WordPress admin panel).

Don’t get me wrong.  There are quite a few plugins that do this, and if you feel more comfortable doing it that way, by all means, add another plugin to the list of things your blog needs to load.  But if you are brave enough to do something quick, easy, and lightweight, by all means read on!

wp_mail and some relatives

WordPress relies on a function called wp_mail to send email.  This function is essentially a wrapper for the phpmailer class.  The problem is that the wp_mail default email “from” address cannot be configured via the WordPress admin panel.  But the fortunate thing is that this function and its related functions are both pluggable and can be filtered.  Our example here is to simply opt for a filter. wp_mail relies on some other outside information, some of which is wp_mail_from (an email address) and wp_mail_from_name (the real name given to the email address).  Since that is all we want to change, we are just going to filter those.

Filtering the email address

Add a filter for the email address using ‘add_filter’:

add_filter( 'wp_mail_from', 'my_mail_from' );
function my_mail_from( $email )
{
	return "change-this-to-your-email-address";
}

Filtering the email name

Now add a filter for the name of the email address:

add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name )
{
	return "My Name";
}

The functions.php file

That’s great, but what do I do with it? I’m glad you asked.  Add these two filters and their accompanying functions to your theme’s functions.php file.  You don’t even need to mess with a file editor and ftp for this.  I would simply go to the Appearance > Editor menu in the WP Admin Panel, then find ‘functions.php’ in the list of theme files on the right.  Add these filters and you are in business.

Easy peasy lemon squeezy.  And no bloated plugin to slow down your site!

If you’d rather use a plugin to handle WordPress email configuration instead of the functions file, that’s covered in this post.

For more information on testing, troubleshooting, and changing your WordPress email configuration for wp_mail, here are some additional posts:

Comments

  1. GG says

    Could you please post a screen shot of what this directory looks like? Or post some url’s where I could see if it is something that will work for us? Thank you!

  2. Donna Maher says

    Chad – you are absolutely amazing. I wish I’d ‘found’ you sooner! I’m going to try out this (and I’m a grandmother-type webmaster) mail function edit – it sounds amazing.

    One question, does it need to be added to every single theme I change to? I’m guessing yes, but thought I’d ask. That’s probably the only disadvantage to it being an edit rather than a plugin.

    I’m also going to test your member plugin and can’t wait to report the results!

    Bless you for being so generous!
    Donna

    • Chad says

      Thanks Donna for the kind words.

      Yes, if you choose to use a process like this in a functions.php file, you would need to add any customized functions to the new theme’s functions.php. It would be just as easy to make this a simple plugin as well. Maybe I’ll put that together in another post ;-)

  3. Bruce C says

    Chad, great plugin. I am also a plugin developer and was looking for something like this. You saved me time, so I will be donating this week.

    A couple of questions and issues:
    1. In the dialogs tab, it says we are allowed simple HTML. No problem. I want registered users to pay a fee to access our WordPress blog. No problem, there. I checked Moderate Registration and we will approve them manually.

    The problem is when I try to add a hyperlink to the “Registration completed” dialog. It seems to take my entire URL of the current blog with it, although I want to add a hyperlink to an external URL. So, if my URL is http://www.domain.com and I hyperlink in the HTML a href=”http://www.domain2.com”, it displays in the dialog box: http://www.domain.com/http://www.domain2.com which is obviously wrong. Can that be fixed in the plugin? Where can I go into the PHP (which plugin file) to remove the current URL?

    • Chad says

      Hi Bruce – thanks for the comments. I see the problem and it is a relatively easy fix. I will make the corrections in the upcoming 2.6 release (I’m a little behind on that, but it should be complete this week).

      In the meantime, the fix is in wp-members-dialogs.php. In the function wpmem_inc_regmessage (about halfway through the file), change this line:

      <p><b><?php echo $wpmem_dialogs[$row+1]; ?></b></p>

      to this:

      <p><b><?php echo stripslashes( $wpmem_dialogs[$row+1] ); ?></b></p>

      And while you’re in there, change:

      <p><b><?php echo $themsg; ?></b></p>

      to this:

      <p><b><?php echo stripslashes( $themsg ); ?></b></p>
  4. Dane says

    Thanks for your info, but when I tried to use it it created a fatal error for me. Well, I created the fatal error, but I’m not familiar enough with this to know what I am doing. I do know, however, that I need to know how to edit the code specific for my email address… which parts do I edit in my site-specific info? I know that makes me sound ignorant of WP… but that’s because I am. Perhaps you could use my email I registered the comment with to demonstrate it for me?

    • Chad says

      add_filter( 'wp_mail_from', 'my_mail_from' );
      function my_mail_from( $email )
      {
          return '[email protected]';
      }
      
  5. Sean says

    Hey Chad,

    Here’s the content of the (theme-function) functions.php. Where do I exactly insert the “add_filter”.

    Thanks,
    Sean

    • Chad says

      Sean – your functions.php file is simply a list of extra functions that load with your WP theme. This can contain hooks and filters that do extra things (such as override the default email address). You can add this anywhere, but typically you will add something new at the bottom of the page, noting that of course all of this needs to go within php delimiters:

       <?php ...code here... ?>

      . Depending on your theme, etc, there may or may not be a closing delimiter at the end of the file. If there is, then put your filters and functions inside the closing delimiter. If not, don’t worry about it, just append to the end.

  6. Barb Peterson says

    Hi Chad,

    Being somewhat new to WordPress, my question: If I were to add this to the theme’s function file, wouldn’t these additions be deleted next time the theme comes out with a new update and I do that update?

    So, for the sake of the new WP people using your wonderful plugin, if I am right in my previous statement, wouldn’t it be great to advise them to save the filter(s) somewhere on their local PC/Mac and after each theme update to go in and add those filter(s) back in?

    Thanks in advance for your response…
    ~Barb

    • Chad says

      Barb – thanks for your comments. A couple of things… It is actually a best practice to keep a local backup of at minimum your theme, and better yet, your entire site, and even better yet, your site plus a database backup. Secondly, while what you noted is generally true, if you are making any customizations to a theme (even as simple as adding a single function), it would be better to establish it as a child theme. That leaves you in a more upgradeable situation (*especially* for users that are using TwentyEleven). But if that’s too much to manage, it’s better to use the instructions outlined as a plugin.

  7. Kasper says

    Thank you very much for these functions, really useful.
    Using them in the functions of my child-theme and all works well.

    However after some searching myself I haven’t managed to locate the function that puts the login link into the email message (the content).

    I would like to make some changes to it, so I wondered if you know which function to override for it.

    Thanks in advance!

    • Kasper says

      Found some plug-ins that can make this happen, however without plugins i’m still clueless as to which functions to change. prefer not to use too many plugins, so if anyone got a clue please enlighten me !

      • Chad says

        Kasper – I’m a little confused. Are you talking about changing the email that is sent out from WordPress when a user is created, or from the WP-Members plugin? Your question isn’t really clear on that.

        Note: you shouldn’t be making changes directly to any functions. Stick to action hooks and filters or pluggable functions. If you don’t know what these terms mean, you should probably do a little homework on those terms in the WordPress Codex before getting under the WP hood.

        If you want to make a change to the email that WordPress sends out to new users, that is wp_new_user_notification and it is pluggable. This means you can write your own function called wp_new_user_notification and WordPress will use yours instead of its own. The original function is in /wp-includes/pluggable.php.

        If you were referring to the email that the WP-Members plugin sends out, that is found in wp-members-email.php. At present, this is not pluggable, and I would not recommend making direct changes to it as they would be overwritten in an upgrade. The upcoming 2.7 release will be including an admin panel to customize all of the emails the plugin sends out.

        • Kasper says

          Awesome Chad thanks alot,

          I was at first thinking about the pluggable wordpress mail indeed, just couldn’t seem to find it. Will try it out now.

          Also looking forward to the 2.7 upgrade, cause I’m not totally sure whether that mail is send by wordpress or the plugin.

          And sorry for my misuse of the terms, I indeed meant to change plugable functions in a seperate file to override rather then to change the core of wordpress itself.

          Again thank you for the support!

          • Kasper says

            Found out its the message send by the wp-members plugin. Good that this will be editable in the next upgrade, I’ll just be patient then.

  8. Bobby says

    Will this also change the “from” email address on the subscription confirmation emails sent out through jetpack subscriptions?

    Thx!
    BP

    • Chad says

      It is a WP filter, so it will change the from address for any message that uses wp_mail (which presumably would include jetpack)

  9. Krister Andersson says

    Notice that if you specify a `mail_from` address that is not associated with your domain it’s a good chance that the mail sent will be put in the junk or spam folder at the receiving end.

    • Chad says

      That’s quite possible – a good way around that is to use the WP Mail SMTP plugin which would allows wp_mail to send via SMTP – that way the email and server in the headers will match (resulting in a lower likelyhook of being flagged as spam).

  10. StefsterNYC says

    This is pure genius Chad. Thanks. How would one do it so you could echo the admin email in settings though?

Trackbacks

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>