如何在 $woocommerce->mailer() 中添加 Reply-To header

How to add Reply-To header in $woocommerce->mailer()

我在下面有一个自定义的 WooCommerce 邮件程序功能,用于向客户发送电子邮件作为购买通知,但我需要添加 reply-to 标签。

详细描述,客户必须从store@mycompany.com收到订单通知的电子邮件($order->billing_email),并且需要为support@mycompany.com附加reply-to标签。

这样做是从 store@mycompany.com 发送电子邮件,但是当客户想问我们任何问题时点击回复,这些回复将转到 support@mycompany.com

任何人都可以帮助我如何更改 $mailer->send 功能来达到要求吗?

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+

在 send() 函数中查看 Class WC_Email 时,您有:

send( string $to, string $subject, string $message, string $headers, string $attachments ) 

将其转换为您的代码,$headers 可以这样使用:

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 );
    }
}

这应该有效。请查看最后的参考代码,因为它与您的非常相似……

参考文献: