I am attempting to programmatically update the "sent from" address for a particular WooCommerce email. Specifically the customer_new_account email. I am aware of the woocommerce_email_from_address filter and I am able to (theoretically) update the address using the following:
function custom_send_from_address ( $from_email, $wc_email ) {
if( $wc_email->id == 'customer_new_account' )
$from_email = 'welcome@mywebsite.org';
return $from_email;
}
add_filter( 'woocommerce_email_from_address', 'custom_send_from_address', 10, 2 );
However, this only seems to update the "reply to" address, not the "sent from" address. We are using the FluentSMTP plugin, and I have added a connection to this email address to the plugin. I’m not sure if I am missing a step somewhere that would give WordPress access to this email address?
It is likely that some other plugin or your custom code may be overwriting the from email address. To resolve this try these options:
Look for the plugins or custom code which is hooking to the filter wp_mail_from
and check if they are altering your from email address. If yes try to disable them.
Add this filter callback to enable filtering the from email address in FluentSMTP plugin. (This is enabled by default, but just making sure if you have not disabled it in some other place)
add_filter( 'fluentsmtp_disable_from_name_email', '__return_false', 100 );
Instead of woocommerce_email_from_address
filter, use wp_mail_from
filter. But the catch here is you will only get the current from email address in the call back. Since you only need to alter the from email address for customer_new_account
emails, you need to add_filter
before the new customer account email is sent and remove it using remove_filter
after the email is sent.
Example:
function custom_send_from_address () {
return'welcome@mywebsite.org';
}
function attach_filter () {
// Hook to filter with low priority
add_filter( 'wp_mail_from', 'custom_send_from_address', 1000 );
}
add_action( 'woocommerce_created_customer_notification', `attach_filter` );
function detach_filter () {
remove_filter( 'wp_mail_from', 'custom_send_from_address' );
}
add_action( `woocommerce_email_sent`, 'detach_filter' );