mimi mimi - 26 days ago 9
PHP Question

PHPMailer adding multiple emails to BCC from MYSQL

In below code, we are first selecting users based on the admin's input from database. Then sending emails to those users. With the code it sends emails to

$mail_news->addAddress('testuser@gmail.com');
test user. But for the bcc part is not working, as it doesn't send any emails to bcc email users.

foreach($email_array as $news_mail){
$mail_news->AddBCC($news_mail.";");
}


This is how we fetch user emails via form & PHP prepare statement with mysqli.

Here is the main part code:

if($msn->execute()){
$msn->store_result();
$msn->bind_result($news_mail);
while($msn->fetch()){
$email_array[] = $news_mail;
}
// echo "successful";
}
else
{
echo "database failed";
}
//--Email Sending Starts
$mail_news = new PHPMailer;
$mail_news->isSMTP();
$mail_news->Host = EMAIL_HOST;
$mail_news->SMTPAuth = true;
$mail_news->Port = EMAIL_PORT;
$mail_news->SMTPSecure = 'tls';
$mail_news->Username = EMAIL_ADD;
$mail_news->Password = EMAIL_PASS;
$mail_news->From = EMAIL_ADD;
$mail_news->FromName = 'Company Account';
$mail_news->addAddress('testuser@gmail.com');
foreach($email_array as $news_mail){
$mail_news->AddBCC($news_mail.";");
}
$mail_news->WordWrap = 50;
// $mail_news->SMTPDebug = 2;
$mail_news->isHTML(true);
$mail_news->Subject = "".$sub;
$mail_news->Body = "".$body;
$mail_news->AltBody = "".$altbody;

if(!$mail_news->send()) {
echo "Failed Sending Emails" ;
echo 'Mailer Error: ' . $mail_news->ErrorInfo;
} else {
echo "All Email sending completed" ;
}

?>
</form>
<?php
$msn->close(); // Finally closing the database
}
?>

Answer Source

You need to change the line $mail_news->AddBCC($news_mail.";"); with $mail_news->AddBCC($news_mail); because the method addBCC() handles the semicolon by itself. You do not need to specify by your own.