如何使用phpmailer

时间:2017-06-28 21:00:51

标签: php email phpmailer

我试图在phpmailer AddCC()中添加多个电子邮件地址。

使用以下代码,我只能在cc中添加一个电子邮件地址。但我想添加从查询中提取的所有电子邮件。

$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);
}

1 个答案:

答案 0 :(得分:1)

创建一个数组来存储多个cc电子邮件。

while ($row = $querycc->fetch_assoc()) {
    $ccemail[]= $row['email'];
    $ccname[]= $row['employee'];
}

这些数组在phpmailer代码中。

或者您可以使用以下代码。

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');
}

在phpmailer中

// loop the array and add to cc
foreach($recipients as $recipient){
   $mail->AddCC($recipient['email'],$recipient['name']);
}
相关问题