Change WordPress Email “Send From” Settings

0
263
Change WordPress Email

One common question I often hear regarding WordPress email configuration is, “How do I Change WordPress Email address from wordpress@mydomain.com?”  I suspect that most people are unaware that WordPress has its own function called wp_mail() for sending email and that it has a default address that it sends email from.  If they are aware, most users opt for changing this with a plugin.

WordPress has no ability to manage email settings through the admin panel, but that does not mean you can’t manage the email configuration at all.  It actually is very flexible.  You can even change 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 (wordpress@yourdomain.com), 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).

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, then 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’:

/* auto-detect the server so you only have to enter the front/from half of the email address, including the @ sign */
function xyz_filter_wp_mail_from($email){
/* start of code lifted from wordpress core, at http://svn.automattic.com/wordpress/tags/3.4/wp-includes/pluggable.php */
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
/* end of code lifted from wordpress core */
$myfront = "whateverIwant@";
$myback = $sitename;
$myfrom = $myfront . $myback;
return $myfrom;
}
add_filter("wp_mail_from", "xyz_filter_wp_mail_from");

Change the Name

Here’s the snippet for changing the “from email name”:

/* enter the full name you want displayed alongside the email address */
/* from http://miloguide.com/filter-hooks/wp_mail_from_name/ */
function xyz_filter_wp_mail_from_name($from_name){
return "Best Name Ever";
}
add_filter("wp_mail_from_name", "xyz_filter_wp_mail_from_name");

Conclusion

Your email recipients (like new member registrations and post edit notifications) will see your plugin’s settings in action.

From “name” and “address” are highlighted by the red outlines

It’s as simple as that. Feel free to share your own experiences below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here