I'm trying to add multiple email address in phpmailer AddCC().
With below code i am able to add only one email address in cc. But i want to add all the emails fetched from the query.
$sqlcc = "SELECT * FROM notificationslist WHERE status='1'";
$querycc = $connect->query($sqlcc);
$num_rowscc = mysqli_num_rows($querycc);
if($num_rowscc>0){
while ($row = $querycc->fetch_assoc()) {
$ccemail= $row['email'];
$ccname= $row['employee'];
}
} else {
$ccemail= 'akash1sethi@gmail.com';
$ccname= 'Akash Sethi';
}
PHP MAILER CODE HERE
$multiplecc = array(
$ccemail => $ccname,
);
foreach ($multiplecc as $ccemail => $ccname)
{
$mail->AddCC(trim($ccemail), $ccname);
}
Create an array to store multiple cc emails.
while ($row = $querycc->fetch_assoc()) {
$ccemail[]= $row['email'];
$ccname[]= $row['employee'];
}
And these arrays in phpmailer code.
Or you can use the below code.
if($num_rowscc>0){
while ($row = $querycc->fetch_assoc()) {
// create an array to have multiple records
$recipients[]= array('email'=>$row['email'],'name'=>$row['employee']);
}
} else {
$recipients[]= array('email'=>'akash1sethi@gmail.com','name'=>'Akash Sethi');
}
In phpmailer
// loop the array and add to cc
foreach($recipients as $recipient){
$mail->AddCC($recipient['email'],$recipient['name']);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.