How to change Sender’s Email in WooCommerce?

WooCommerce -> Settings -> EmailS -> the first two options, "FROM: Name, FROM: Email", are the Sender’s email and name. When an order is placed a notification email is sent to both Shop Manager and Customer from the same Sender’s email and name (which we set from admin dashboard).

When Customer replies to that email, he basically replies to Shop Manager and it works fine.

Read More

But Shop Manager receives the notification email from his (given) email address, in actually there would be client’s email.

P.S: I want that Shop Manager gets the email from the customer’s billing email and customer gets it from Shop Manager’s given email.

https://wordpress.org/plugins/woocommerce/

Related posts

3 comments

  1. The only way I can think of to achieve this is to programmatically generate the Shop Manager notification email using one of the system action hooks. Since there is only one field in the “from” settings, it will be used (by default) for all the outbound messages.

    That said, check out the Code Snippets plugin at https://wordpress.org/plugins/code-snippets/. You can put some code here (and it will be saved to the DB, and included the system code at runtime) that could generate this programmatic email when, say, a new order is generated.

    Hope it helps.

  2. After going through your question it made sense that for a shop owner it needs to be like the mail should have sent from address as the client’s email address, so that he can reply to each order in a single email thread.

    //filter for the "mail from" email address which is set in WooCommerce Settings Email Tab-> Email Sender Options -> "From" Email Address which is generally set to the shop admin email address or a support team email address.
    add_filter('wp_mail_from', 'wdm_sent_from_email', 99, 1);
    
    //function which will change the mail from email address to the the Customer's billing address if the mail this filter is running which sending mail to the admin.
    function wdm_sent_from_email( $sent_from = '' ) {
        //check whether our custom parameter is set or not.
        if ( isset($_POST['wdm_sent_to_admin']) ) {
    
        //check whether the mail is sent to the admin and other recieptent other than customer
        if ( $_POST['wdm_sent_to_admin'] ) {
            //set it to the customer's billing email
            $sent_from = $_POST['billing_email'];
    
            //set this parameter back to false
            $_POST['wdm_sent_to_admin'] == false;
        }
        }
        return $sent_from;
    }
    
    //filter for email from name
    add_filter('wp_mail_from_name', 'wdm_sent_from_name', 99, 1);
    
    function wdm_sent_from_name( $sent_from_name = '' ) {
        //check whether our custom parameter is set or not.
        if ( isset($_POST['wdm_sent_to_admin_from_name']) ) {
    
        //check whether the mail is sent to the admin and other recieptent other than customer
        if ( $_POST['wdm_sent_to_admin_from_name'] ) {
    
            //set sent mail from name eg. "Website-Name customer"
            $sent_from_name              = wp_specialchars_decode(esc_html(get_option('woocommerce_email_from_name')), ENT_QUOTES) . " customer";
    
            //set this parameter back to false
            $_POST['wdm_sent_to_admin_from_name']    = false;
        }
        }
        return $sent_from_name;
    }
    
    //action were we will set and the parameter to indicate whether the mail is sent to admin or customers.
    add_action('woocommerce_email_header', 'wdm_header_function', 99, 1);
    
    function wdm_header_function( $email_heading ) {
    
        if ( $email_heading == 'New customer order' ) {
    
        //parameter to indicate whether to change the from email in the mail.
        $_POST['wdm_sent_to_admin'] = true;
    
        //parameter to indicate whether to change the from name in the mail.
        $_POST['wdm_sent_to_admin_from_name'] = true;
    
        //Just to indicate in mail sent to admin that the sent from email is set in code.
        echo "<b>Its Because you have chosen to have this email sent from Customer's Email id.</b>";
        } else {
    
        //parameter to indicate whether to change the from email in the mail.
        $_POST['wdm_sent_to_admin'] = false;
    
        //parameter to indicate whether to change the from name in the mail.
        $_POST['wdm_sent_to_admin_from_name'] = false;
        }
    }
    

    Note : This might give you and alert message while viewing the message at your side only saying “This message may not have been sent by: (your customer’s billing address) ” as this is not really sent by a customer to you.

    Try out the above code and lemme know whether it works for you and accomplishes your purpose.

  3. I think you can also use wordpress hooks for that:

    // Function to change email address
    
    function wpb_sender_email( $original_email_address ) {
        return 'tim.smith@example.com';
    }
    
    // Function to change sender name
    function wpb_sender_name( $original_email_from ) {
        return 'Tim Smith';
    }
    
    // Hooking up our functions to WordPress filters 
    add_filter( 'wp_mail_from', 'wpb_sender_email' );
    add_filter( 'wp_mail_from_name', 'wpb_sender_name' );
    

Comments are closed.