I had a custom WooCommerce mailer function below for sending emails to customers as a notification of there purchase, but I got a requirement to add reply-to tag.
To describe in detail, customer has to get email($order->billing_email
) for the order notification from store@mycompany.com
and need to append reply-to tag for support@mycompany.com
.
What this does is that emails will be send from store@mycompany.com
, but when customers hit reply when they want to ask us any questions, these replies will go to support@mycompany.com
Can any one help me how to change the $mailer->send
function to achieve the requirement ?
function my_awesome_publication_notification($order_id, $checkout=null) {
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'completed' ) {
// Create a mailer
$mailer = $woocommerce->mailer();
$message_body = __( 'Hello world!!!' );
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
// Client email, email subject and message.
$mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
}
}
}
Added Compatibility for Woocommerce 3+
When looking at Class WC_Email at the send() function you have:
send( string $to, string $subject, string $message, string $headers, string $attachments )
Transposing this to your code, $headers could be used this way:
function my_awesome_publication_notification($order_id, $checkout=null) {
global $woocommerce;
// Get order object.
$order = new WC_Order( $order_id );
$order_status = method_exists( $order, 'get_status' ) ? $order->get_status() : $order->status;
if( $order_status === 'completed' ) {
// Create a mailer
$mailer = $woocommerce->mailer();
$message_body = __( 'Hello world!!!' );
// Message head and message body.
$message = $mailer->wrap_message( sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
// Here is your header
$reply_to_email = 'support@mycompany.com';
$headers = array( sprintf( 'Reply-To: %s', $reply_to_email ) );
// Or instead, try this in case:
// $headers = 'Reply-To: ' . $reply_to_email . '\r\n';
// Client email, email subject and message (+ header "reply to").
$mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message, $headers );
}
}
This should work. Please take a look to last reference code, as it's very similar to yours…
References: