PHP / WordPress sending email to multiple addresses

I am trying this on wordpress, trying to email multiple email address. I have 2 email addresses in the database.

Here is the code:

Read More
$r = $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_users", $ID, $user_email ));

foreach ($r as $row) {

$to       = 'someone@myhost.com';
$bcc = $row->user_email;
$subject  =  $_POST["subject"];
$message  =  $_POST["message"];
$headers  = 'From: me@mymail.com' . "rn" .
            'Reply-To: me@mymail.com' . "rn" .
            'MIME-Version: 1.0' . "rn" .
            'Content-type: text/html; charset=iso-8859-1' . "rn" .
            'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
    echo "Email sent";
}    
else {
    echo "Email sending failed";
}

It’s sending emails BUT what’s happening is that the TO (someone@myhost.com) is getting 2 emails and the $bcc is getting none.

What am I doing wrong here?

Related posts

Leave a Reply

1 comment

  1. Yep, this behavior it is pretty normal, you forgot to put in $headers the Bcc: part, it should be like:

    $headers  = 'From: me@mymail.com' . "rn" .
                'Reply-To: me@mymail.com' . "rn" .
                'MIME-Version: 1.0' . "rn" .
                'Bcc: '.$bcc. "rn".
                'Content-type: text/html; charset=iso-8859-1' . "rn" .
                'X-Mailer: PHP/' . phpversion();