5

Is there a hook that would allow me to email multiple email addresses for the default admin email notifications?

I was hoping I could build an array:

$adminEmails = array('me1@example.com', 'me2@example.com');

Then have all admin emails (like new user notifications) sent to $adminEmails

Possible?

3

Try this:

update_option( 'admin_email', 'me1@example.com, me2@example.com' );

Note that the value is a string; open and close quotes only!

  • Thanks @shanebp - but it doesn't work. It just merges them into a single string in the options, so it tries sending to: me1@example.comme2@example.com – Pat Gilmour Dec 19 '14 at 2:30
  • That's too bad... So, for just admin emails re new users, the function is pluggable. So you could create a version that sends to multiple hardcoded emails re admins. codex.wordpress.org/Function_Reference/wp_new_user_notification – shanebp Dec 19 '14 at 15:17
  • Thanks shanebp - I think what you suggest is actually the best solution for my needs. – Pat Gilmour Dec 19 '14 at 23:24
  • @PatGilmour Then please mark it as solution. – kaiser Mar 17 '16 at 0:21
  • @PatGilmour Well if the original answer is incorrect it probably shouldn't be marked as correct. I would either mark sMyles's answer correct or update shanebp's answer with the solution offered in the comments. – Simon East Jan 12 '17 at 1:52
4

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

  • One thing to be careful of, say you request password reset and your email is the same as the admin_email option, those other emails will get that email as well ... just keep that in mind, it only checks the TO when emails are sent, doesn't check if it's supposed to be an admin email (as I dunno how to even do that right now) – sMyles Mar 17 '16 at 0:43
1

Here's my solution, it uses the update_option_* filter, I believe it to be the right way to go here. add this to a plugin or your theme functions.php file, and then you can safely put comma separated admin emails in the settings->general screen.

add_filter('pre_update_option_admin_email','sanitize_multiple_emails',10,2);

function sanitize_multiple_emails($value,$oldValue)
{
    //if anything is fishy, just trust wp to keep on as it would.
    if(!isset($_POST["admin_email"]))
        return $value;

    $result = "";
    $emails = explode(",",$_POST["admin_email"]);
    foreach($emails as $email)
    {
        $email = trim($email);
        $email = sanitize_email( $email );

        //again, something wrong? let wp keep at it.
        if(!is_email($email))
            return $value;
        $result .= $email.",";

    }

    if(strlen($result == ""))
        return $value;
    $result = substr($result,0,-1);

    return $result;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.