【问题标题】:How to add multiple cc email address using phpmailer如何使用 phpmailer 添加多个抄送电子邮件地址
【发布时间】:2017-06-29 05:18:43
【问题描述】:

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

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

$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 邮件代码

$multiplecc = array(
    $ccemail => $ccname,
    );


foreach ($multiplecc as $ccemail => $ccname)
{
    $mail->AddCC(trim($ccemail), $ccname);
}

【问题讨论】:

  • 您的方法有效,您确定添加的值正确吗?您要添加多少?

标签: php email phpmailer


【解决方案1】:

创建一个数组来存储多封抄送电子邮件。

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

【讨论】:

  • @rtfm 在 while 循环中他没有创建数组,他直接将值分配给变量,这将只有最后的结果。
猜你喜欢
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2021-04-29
  • 2021-09-13
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
相关资源
最近更新 更多