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.