This can be done by filtering the wp_mail
function, checking if to
is set to the admin email, and if so, add your additional emails addresses, and return args back to wp_mail
add_filter( 'wp_mail', 'my_custom_to_admin_emails' );
/**
* Filter WP_Mail Function to Add Multiple Admin Emails
*
*
*
* @param array $args A compacted array of wp_mail() arguments, including the "to" email,
* subject, message, headers, and attachments values.
*
* @return array
*/
function my_custom_to_admin_emails( $args ) {
// If to isn't set (who knows why it wouldn't) return args
if( ! isset($args['to']) || empty($args['to']) ) return $args;
// If TO is an array of emails, means it's probably not an admin email
if( is_array( $args['to'] ) ) return $args;
$admin_email = get_option( 'admin_email' );
// Check if admin email found in string, as TO could be formatted like 'Administrator <admin@domain.com>',
// and if we specifically check if it's just the email, we may miss some admin emails.
if( strpos( $args['to'], $admin_email ) !== FALSE ){
// Set the TO array key equal to the existing admin email, plus any additional emails
//
// All email addresses supplied to wp_mail() as the $to parameter must comply with RFC 2822. Some valid examples:
// user@example.com
// User <user@example.com>
$args['to'] = array( $args['to'], 'another@domain.com', 'Admin4 <admin4@domain.com>' );
}
return $args;
}
We return the TO as an array, as wp_mail
will handle the array and explode it as needed to send the email