Get wordpress to email more than one email address for comment notifications

OK so i recently got a request from a client where the client wanted new comment notifications to go out to five separate email address’s.
Firstly i tried typing the email address’s into the General settings Email address field and then separating each address with a comma or semi colon, but WordPress removed the additional comma’s and semi colons.

Then after doing some research i came across something on the wordpress.org website.
The below is a simple function that send an email out to specified email address’s.

function email_friends( $post_ID ){
    //set subject for email
    $subject = "New comment posted on Website";
    //set headers
    $headers = "From: website@website.comrn";
    $headers .= "Reply-To: website@website.comrn";
    $headers .= "MIME-Version: 1.0rn";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1rn";
    //Set email addresses
    $to = 'email1@domain.com, email2@domain.com, email3@domain.com';
    //create the content for the mail
    $message = "Feedback - new comment. <br />Hi, a new comment was just posted on the website.<br />Thank you.";
    
    //Send email using wordpress mail function
    wp_mail( $to, $subject, $message, $headers );
    
    //return the post id incase you wish to use it
    return $post_ID;
}

Now we simply call the above function when a comment is posted by using wordpress’s add_action();

add_action('comment_post', 'email_friends');

Now every time a comment is posted, the notification will be sent out to all the email address’s listed in the function.

Hope this helps, Nathaniel.

%d bloggers like this: