Search code examples
phpwordpresswoocommerceemail-headersemail-notifications

Custom "reply to" email header in Woocommerce New Order email notification


I'm looking to filter the email headers of the new order form in woocommerce. I'm trying to replace the customer email address with the main site admin email address. We need to do this because Gmail is flagging new orders as spam because the from address is not the same as the reply to address. The function below works partially:

add_filter( 'woocommerce_email_headers', 'woo_add_reply_to_wc_admin_new_order', 10, 3 );

function woo_add_reply_to_wc_admin_new_order( $headers = '', $id = '', $order ) {
if ( $id == 'new_order' ) {
    $reply_to_email = get_option( 'admin_email', '' );
    $headers .= "Reply-to: <$reply_to_email>\r\n";
}
return $headers;}

This function is adding the site admin email address but doesn't remove the customer email.

Anyone out there have any ideas on how to remove the customer email from the reply to field? Note: We still need to have a record of the customer email on the order - we just don't want it in the email headers

Any help would be great!


Solution

  • The correct way to make it work is:

    add_filter( 'woocommerce_email_headers', 'new_order_reply_to_admin_header', 20, 3 );
    function new_order_reply_to_admin_header( $header, $email_id, $order ) {
    
        if ( $email_id === 'new_order' ){
            $email = new WC_Email($email_id);
            $header = "Content-Type: " . $email->get_content_type() . "\r\n";
            $header .= 'Reply-to: ' . __('Administrator') . ' <' . get_option( 'admin_email' ) . ">\r\n";
        }
        return $header;
    }
    

    This code goes on function.php file of your active child theme (or theme). Tested and works.