Skip to content

Instantly share code, notes, and snippets.

@butlerblog
Last active July 19, 2021 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save butlerblog/3ab9367e669d4f3b30c0b17b9e140c18 to your computer and use it in GitHub Desktop.
Save butlerblog/3ab9367e669d4f3b30c0b17b9e140c18 to your computer and use it in GitHub Desktop.
#wp_mail add bcc based on contents of subject line
<?php // DO NOT include this line. Add below to your theme functions.php
/**
* Add bcc address to email based on contents
* of the email subject line.
*
* Set email address and content of the subject
* line for the email being filtered.
*/
add_filter( 'wp_mail', 'custom_mails' );
function custom_mails( $args ) {
// What email to bcc?
$bcc_email = sanitize_email( 'example@email.com' );
// Welcome email subject line (or specific static portion of the subject line).
$welcome_email_subj = "Welcome email subject";
if ( strpos( $args['subject'], $welcome_email_subj ) ) {
if ( is_array( $args['headers'] ) ) {
$args['headers'][] = 'Bcc: ' . $bcc_email;
} else {
$args['headers'] .= 'Bcc: ' . $bcc_email . "\r\n";
}
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment