Software, Development, and WordPress

Change the Email Sender in WordPress

If you're an experienced developer, you can skip to the code by clicking here.

If you’re in the business of hosting WordPress-based sites or at least managing WordPress-based sites, then it’s likely that you’re also responsible for managing email accounts or how email is relayed from the site and the server to its visitors.

And depending on the nature of your setup, this may not be a problem; however, if you’re operating on some type of hosted solution, some type of managed hosting, or aren’t using a third-party solution such as Google Apps, then there’s a chance that site visitors are receiving emails from their favorite WordPress-based site with incorrect or unclear email sender information.

Case in point: I manage a site where comment notification emails were being sent with the following formation:

[ Site Name ] [My Email Address] via [Server Name ]

Obviously, this is okay if it’s my site, but a client’s site? Negative.

To be clear, this was not a simple matter of changing out administrative information in the WordPress settings. This had more to do with a combination of relaying email from the server and some of the server configuration settings.

Programmatically Change Email Sender in WordPress

First, setup a custom filter for the wp_mail_from hook. In the function, set the email address that you wish to send as the reply address:

function example_from_email( $email ) {
    return 'ted@mosby.com';
} // end example_from_email
add_filter( 'wp_mail_from', 'example_from_email' );

Note that the function above accepts a single parameter which is usually the email address from which it’s originally going to use as the sender address.

In this case, we simply return a custom string.

Next, we need to setup a custom filter for wp_mail_from_name:

function example_from_name( $name ) {
    return 'Teddy Westside';
} // end example_from_name
add_filter( 'wp_mail_from_name', 'example_from_name' );

This particular function will typically send the author’s name (or possibly the blog name depending on the configuration). To override, you simply return a custom string.

Obviously, this is much more extensible especially if you were to introduce an option page in a plugin or something similar. At any rate, it’s obviously relatively easy to change the sender email information in WordPress.

19 Comments

  1. Jason Bradley

    Good stuff. Thanks Tom!

  2. Elliott Richmond

    Neat tip, I’ve used this many times – is there a way to change the content of the email when a user registers? I’ve tried using new_user_email_content but it fails.

  3. Adam Kowalski

    if you didn’t find anything yet, you might be interested of this:

    PS – I know my English is great: P

  4. Justin

    Too many blog posts out there say to edit things in wp-includes which is just silly. Thanks for this! Glad I found it.

    • Tom McFarlin

      Definitely – glad to help!

      • Mrina

        Hello Tom, i want to get the email of sender and not my email again in order to be able to replay , please help coz i see only the organization name, subject and my email NOT the sender email.

        • Tom

          Hey Mrina,

          Unfortunately, this isn’t something that I can’t typically answer via comments as it gets a little more involved than a couple of sentences.

          Best of luck!

  5. TARA

    I CAN’T FIGURE OUT HOW TO DO THIS

  6. Lou

    When I add this to my theme, the email goes to spam folder… :(

    • Tom McFarlin

      This is the first I’ve heard of this happening, and I’m unable to reproduce this. Sorry about that!

      Try experimenting with changing the sender to see if that impacts it at all.

  7. chad

    Tom,

    This looks like exactly what I’m looking for my site. When I added your code to functions.php file, my site went blank. I just copied and pasted to the end of the functions.php file. Did I miss something or do something wrong? I can’t seem to figure it out.

    Thanks

    • Tom

      When this happens, it normally means there’s a problem in the code such as a missing semicolon, incorrectly placed bracket, or something like that.

      I double-checked the code that I have in the post and it appears to be fine, so the next best bet would be to double-check what you’ve placed in your installation. Start by commenting both functions and then uncommenting each one separately to see if you can recreate the problem.

  8. Simone ReF

    Just a tip.. the anchor link at the beginning of the article doesn’t work :)

  9. Lars

    Tom,

    It’s always nice to find what I’m looking for on your site :)

    -Lars

  10. Steve

    Excellent, thank you! Just added these codes to the functions.php and all work.

    function example_from_email( $email ) {

    return ‘MyEmailAddress’;

    } // end example_from_email

    add_filter( ‘wp_mail_from’, ‘example_from_email’ );

    function example_from_name( $name ) {

    return ‘MyCompanyName’;

    } // end example_from_name

    add_filter( ‘wp_mail_from_name’, ‘example_from_name’ );

  11. Steve

    Hey,

    but how can set up that shows sender email address on sender email field? So I could click “reply” and takes his/her address (not mine).

    Thx,

    Steve

Leave a Reply

© 2020 Tom McFarlin

Theme by Anders NorenUp ↑