butlerblog
12/14/2018 - 8:18 PM

wp_mail() add bcc based on contents of subject line

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