Satch3000
Satch3000

Reputation: 49404

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:

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

foreach ($r as $row) {

$to       = '[email protected]';
$bcc = $row->user_email;
$subject  =  $_POST["subject"];
$message  =  $_POST["message"];
$headers  = 'From: [email protected]' . "\r\n" .
            'Reply-To: [email protected]' . "\r\n" .
            'MIME-Version: 1.0' . "\r\n" .
            'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
            '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 ([email protected]) is getting 2 emails and the $bcc is getting none.

What am I doing wrong here?

Upvotes: 1

Views: 887

Answers (1)

Bud Damyanov
Bud Damyanov

Reputation: 31889

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

$headers  = 'From: [email protected]' . "\r\n" .
            'Reply-To: [email protected]' . "\r\n" .
            'MIME-Version: 1.0' . "\r\n" .
            'Bcc: '.$bcc. "\r\n".
            'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

Upvotes: 3

Related Questions