1

I am currently using the below code to email users when a new post is published, but I need it to BCC all users and not TO all users. Any ideas?

function email_members($post_ID)  {
global $wpdb;
$usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");
$users = implode(",", $usersarray);
mail($users, "New WordPress recipe online!", 'A new recipe have been published on http://www.wprecipes.com');
return $post_ID;
}

add_action('publish_post', 'email_members');

1 Answer 1

3

First: Don’t use mail(). Use wp_mail() instead.

wp_mail( 
    // Send it to yourself
    get_option( 'admin_email' ), 
    'Your subject', 
    'Your message', 
    // extra headers
    array (
        'Bcc:' . implode( ",", $usersarray ),
        'From:' . get_option( 'admin_email' )
    ) 
);
2
  • So is this the way it should look? function email_members($post_ID) { global $wpdb; $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;"); $users = implode(",", $usersarray); wp_mail( // Send it to yourself get_option( 'admin_email' ), 'Your subject', 'Your message', // extra headers array ( 'Bcc:' . implode( ",", $usersarray ), 'From:' . get_option( 'admin_email' ) ) ); return $post_ID; } add_action('publish_post', 'email_members');
    – Shae
    Commented Mar 19, 2012 at 12:19
  • @Shae Yeah, seems to be okay. You don’t have to return anything on an action.
    – fuxia
    Commented Mar 19, 2012 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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