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 Replies to “Change the Email Sender in WordPress”

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

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

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

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

Leave a Reply

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