dtny30176 2016-07-28 09:14
浏览 20
已采纳

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

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

   }
}
  • 写回答

1条回答 默认 最新

  • dqluw20882 2016-07-29 00:50
    关注

    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 . '
    ';
    
            // 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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图