Search code examples
phpphpmailer

Send two email ids in CC


I am obtaining two email id's from database by a query. Both stored in a single variable. I want to send email to these two addresses by PHPMailer keeping them in cc. Currently only one email is being selected and passed in cc. Can I know where am I going wrong. My code here,

  $get_cc_email_id_sql=mysql_query("select * from tbl_name where column_name IN(13,5)");
          $user_email_cc='';
          while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
          {

           $user_email_cc=$get_data_cc['email'];

          } 
$mail = new PHPMailer();
$subject = "Mail";  
$content ="XYZ";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port     = 465;  
$mail->Username = "[email protected]";   // Changed username and password from 
$mail->Password = "xyz";
$mail->Host     = "ssl://smtp.xyz.com";
$mail->Mailer   = "smtp";
$mail->SetFrom("[email protected]", "XYZ");
$mail->AddAddress([email protected]);
$mail->AddCC($user_email_cc);
$mail->Subject = $subject;
$mail->WordWrap   = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);  
if(!$mail->Send()) 
    echo "Problem sending mail.";   
else      
  echo "Mail Sent"; 

Solution

  • use $user_email_cc as array then it will store you both email a 0 and 1 position

    $user_email_cc=array();
              while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
              {
    
               $user_email_cc[] =$get_data_cc['email'];
    
              } 
    

    New Code

    $get_cc_email_id_sql=mysql_query("select * from tbl_name where column_name IN(13,5)");
             $user_email_cc=array();
              while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
              {
    
               $user_email_cc[] =$get_data_cc['email'];
    
              } 
    $mail = new PHPMailer();
    $subject = "Mail";  
    $content ="XYZ";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = TRUE;
    $mail->SMTPSecure = "ssl";
    $mail->Debugoutput = 'html';
    $mail->Port     = 465;  
    $mail->Username = "[email protected]";   // Changed username and password from 
    $mail->Password = "xyz";
    $mail->Host     = "ssl://smtp.xyz.com";
    $mail->Mailer   = "smtp";
    $mail->SetFrom("[email protected]", "XYZ");
    foreach($user_email_cc as $email_cc){
     $mail->AddCC($email_cc);
    }
    $mail->AddAddress([email protected]);
    $mail->Subject = $subject;
    $mail->WordWrap   = 80;
    $mail->MsgHTML($content);
    $mail->IsHTML(true);  
    if(!$mail->Send()) 
        echo "Problem sending mail.";   
    else      
      echo "Mail Sent";