wordpressemaileventswoocommercebcc

How to add BCC recipient to all emails sent by WooCommerce?


I'm trying to add BCC to every mail that is sent by woocommerce / wp. I tried using different solution found on the web and at Stackoverflow and added the snippets to the functions.php of the theme I'm using:

add_filter( 'woocommerce_email_headers', 'add_bcc_to_wc_admin_new_order', 10, 3 );
function add_bcc_to_wc_admin_new_order( $headers = '', $id = '', $wc_email = array() ) {
    if ( $id == 'new_order' ) {
        $headers .= "Bcc: my@mail.net\r\n";
    }
return $headers;
}

and

add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 2);

function add_bcc_all_emails($headers, $object) {

    $headers = array();
    $headers[] = 'Bcc: my@mail.net';
    $headers[] = 'Content-Type: text/html';

    return $headers;
}

and

add_filter('wp_mail','custom_mails', 10,1);

function custom_mails($args){
    $bcc_email = sanitize_email('my@mail.net');

    if (is_array($args['headers'])){
        $args['headers'][] = 'Bcc: '.$bcc_email ;
    }
    else{
        $args['headers'] .= 'Bcc: '.$bcc_email."\r\n";
    }

    return $args;
}

I'm using "The Events Calendar" so I also tried this:

add_action( 'event_tickets_rsvp_tickets_generated', 'tribe_tickets_cc_organizer', 10, 3 );

function tribe_tickets_cc_organizer( $order_id = null, $post_id = null, $attendee_order_status = null ) {
    $to = tribe_get_organizer_email( $post_id, false );
    // bail if there's no valid email for the organizer
    if ( ! is_email( $to ) ) return;
        $event_name        = get_the_title( $post_id );
        $site_name         = get_bloginfo( 'name' );
        $attendee_list_url = admin_url( 'edit.php?post_type=tribe_events&page=tickets-attendees&event_id=' . $post_id ); 
        $content     = '<a href="' . esc_url( $attendee_list_url ) . '" style="color: #000; font-family: sans-serif;">Check the event attendee list</a>';
        $headers     = array( 'Content-type: text/html' );
        $subject     = sprintf( __( 'Your event %1$s has new attendee(s) - %2$s', 'tribe-extension' ), $event_name, $site_name );
        wp_mail( $to, $subject, $content, $headers);
    }

add_action( 'event_ticket_woo_attendee_created', 'tribe_woo_compat_cc', 10, 4 );

function tribe_woo_compat_cc ( $attendee_id, $event_id, $order, $product_id ) {
    tribe_tickets_cc_organizer( null, $event_id );
}

All of theme don't work as intended. They ignore the BCC and send out all emails again, so the admin and the user receive the mails double. But the added bcc mail doesn't receive one mail. I can't figure out, why this is isn't working. Anybody an idea?

Thanks in advance.


Solution

  • Maybe you can have a try with:

    function add_bcc_all_emails( $headers, $object ) {
    
        $headers = array( 
             $headers,
             'Bcc: Me <my@mail.net>' ."\r\n",
        );
    
        return $headers;
    }
    add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 2 );