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.

Category:
Tips
Tags:

Join the conversation! 19 Comments

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

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

    PS – I know my English is great: P

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

  4. I CAN’T FIGURE OUT HOW TO DO THIS

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

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

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

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

  8. Tom,

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

    -Lars

  9. 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’ );

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.