Removing the “From: ” Text in the Email Sender Field

I’m working on email functionality throughout the WordPress Multisites network that I am using. Recently, we also adopted a new transactional email solution through a company called Socket Labs. I will explain more why we transitioned from more popular solutions to Socket Labs in a later post.

To use the Socket Labs service with my websites, I installed their plugin. I was running into an issue with their plugin which actually revealed an issue with my code which appears to be employing a deprecated (obsolete) practice related to the wp_mail() function.

Back Story on Why I Care about “From: “

In an effort to customize everything about our user experience, it is important to ensure that our “from” email field be set to reflect an in-house name and email address, like this:

New Beginnings <office@example.com>

This avoids the default WordPress values for sender name and email address:

WordPress <wordpress@example.com>

When switching from a white-label SMTP email plugin (which helped improve email reputation being sent from my website) to the SocketLabs’ proprietary plugin, a quirk in my emails was introduced. Strangely, it only showed up on some of my websites from within the Multisite network. Sudden the sender line of my emails was showing as follows:

From: New Beginnings <office@example.com>

Note the added “From: ” in the front of the sender name. Now this is a very specific bug caused by a very specific plugin, but it revealed an issue in my code and some other plugin code that was employing the same practice.

Looking at the WP_MAIL() Code

From within WordPress code, when I have wanted to add a sender’s info via the
wp_mail() function, I was accustomed to adding the sender’s info into the $headers array variable, like this:

$headers[] = 'From: New Beginnings <office@example.com>';

However, in the source code for the wp_mail() function, there is a comment right above the line that processes this code:

// Mainly for legacy -- process a From: header if it's there

I’m not trying to write legacy code. So inserting the “From” string as an element in the $headers array doesn’t appear to be best practice.

Using Filters to Assign Sender in WP_Mail

There are two simple filters used in the wp_mail() function that replace the former solution: wp_mail_from and wp_mail_from_name.

//Customize the emails being sent from the system. 
add_filter( 'wp_mail_from', function( $name ) {
	return 'office@example.com';
});

add_filter( 'wp_mail_from_name', function( $name ) {
	return 'New Beginnings';
});

This fixed my issue with the Socket Labs plugin, because this filter gets applied just before an email is actually sent. It also created a uniform experience for all emails being sent from any given site. Depending on the situation, these filters were added in a child theme’s functions.php file or at the root level in an active custom plugin for the site.

Leave a comment

Your email address will not be published. Required fields are marked *