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: example@mail.com', "bcc: maik@gmail.com"], $attachments = array() ) {
But nothing seems to be happening.
What am I missing?
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: example@mail.com, maik@gmail.com';
} else {
$args['headers'] .= "Bcc: example@mail.com, maik@gmail.com\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( 'foo@example.com', 'test', 'test', [ 'From: no-reply@example2.com' ] )
— 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. 🙂