BCC all outgoing emails wordpress

1.7k views Asked by At

So I want to BCC all outgoing emails from my wordpress site to two static, hard coded email addresses.

I went to the pluggable.php file and hard coded the BCC headers to the wp_mail() function like this:

    function wp_mail( $to, $subject, $message, $headers = ['Bcc: [email protected]', "bcc: [email protected]"], $attachments = array() ) {

But nothing seems to be happening.

What am I missing?

1

There are 1 answers

3
Sally CJ On BEST ANSWER

Please do not edit core WordPress files!

Instead, use the relevant hook like wp_mail in your case.

Here's an example and this code would be added into the theme functions file, or you can add it as a Must Use plugin:

add_filter( 'wp_mail', 'my_wp_mail_args' );
function my_wp_mail_args( $args ) {
    // Just replace the email addresses with the correct ones. And note that you
    // don't have to add multiple Bcc: entries - just use one Bcc: with one or
    // more email addresses separated by a comma - Bcc: <email>, <email>, ...

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

    return $args;
}

PS: If you added that as a Must Use plugin, don't forget to add the <?php at the top.

And BTW, just to explain the "nothing seems to be happening", it's because the $headers (fourth parameter) value there can be overridden when the function is called, e.g. wp_mail( '[email protected]', 'test', 'test', [ 'From: [email protected]' ] ) — the $headers is set, hence the default value is not used.

So I hope this answer helps, and keep in mind, never edit any core WordPress files! First, because in many WordPress functions (and templates as well) there are hooks that you can use to modify the function/template output, and secondly, your edits will be gone when WordPress is updated.