mimi
mimi

Reputation: 365

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('[email protected]'); 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('[email protected]');       
      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 
       }
 ?>

Upvotes: 2

Views: 8188

Answers (1)

Sahil
Sahil

Reputation: 462

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.

Upvotes: 4

Related Questions