0

I am trying to add a filter to change my New Order and Shipped emails reply-to addresses for customers only. I tried using this solution Change "reply to" email address in all Woocommerce emails notifications. It works for the customer but now the store owner email doesn't get any New order email. I suppose that the store owner email is not getting the New Order emails because its the same reply-to address. Is there a way to change the reply-to only for the customer's New Order and Shipped emails?

Please help!

Beniamin H
  • 2,048
  • 1
  • 11
  • 17
Vr391
  • 3
  • 2
  • Why does the reply to email need to be different from the sender's email? – helgatheviking Oct 29 '19 at 17:30
  • Because I wont be using that email. – Vr391 Oct 29 '19 at 23:26
  • But why not put your email as the "from" email and avoid the need to code something at all? – helgatheviking Oct 30 '19 at 00:21
  • 1
    @helgatheviking - because when using phpmail (the default), it is spoofing the from address and gets rejected for DMARC reasons if you happen to make the from address be a yahoo address. Also, if you want to set up an SMTP address to use as a mailbot for your whole wordpress site that is a noreply address, it is nice to have a separate reply-to at an address that is attended to. – pathfinder Mar 24 '21 at 16:50

1 Answers1

1

This should prevent adding the reply to email header if it's the same as the recipient:

/**
 * Change reply to email address for customer emails.
 *
 * @param  array $header - The email headers.
 * @param  string $email_id
 * @param  object $order WC_Order
 * @param  object $email - The WC_Email class object for this particular email.
 * @return array
 */
function change_reply_to_email_address( $header, $email_id, $order, $email ) {

    // HERE below set the name and the email address
    $reply_to_name  = 'Jack Smith';
    $reply_to_email = 'jack.smith@doamin.tld';

    // Set the reply to email only if it's not one of the recipients.
    if( false !== strpos( $reply_to_email, $email->get_recipient() ) ) {
        $header  = "Content-Type: " . $email->get_content_type() . "\r\n";
        $header .= 'Reply-to: ' . $reply_to_name . ' <' . $reply_to_email . ">\r\n";
    }

    return $header;
}
add_filter( 'woocommerce_email_headers', 'change_reply_to_email_address', 10, 4 );

There doesn't seem to be a way to check if the email is an admin email or not, so alternatively you could conditionally check for specific email IDs.

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Hello, I added the code but can't figure out the "if" part. I didn't do any changes to that part and only receive a successful email to my admin email but didn't got anything in the customers email. hope you can help me, thanks in advance! – Vr391 Oct 30 '19 at 00:25
  • Hmm... I think you are right and the conditional IF logic looks off. I made an edit, but unfortunately can't really test it right now. If this still doesn't work, you could use something like `in_array( $email_id, array( 'new_order', 'customer_completed_order' ) )` where the array holds all the IDs of the customer emails. – helgatheviking Oct 30 '19 at 00:36
  • Great! If it works, would you mind accepting this as the answer. – helgatheviking Oct 30 '19 at 14:44