douye2110 2016-08-07 12:42
浏览 56
已采纳

为什么在foreach中使用PHP Mailer循环遍历所有收件人将无法正常工作? 我的forearch循环有什么问题?

I'm trying to send an email to each recipient of a list (the recipient is taken from a database query, and the email is sent to all that are "checked", using $_POST['enviar'] in each checkbox to reference each one.

The code does work as intended when using mail(), but it doesn't when I'm using PHPMailer, it just send the email to the first recipient.

So I assume that I'm doing the foreach wrong? How may I correct it?

  if ($correo=mysqli_fetch_array(mysqli_query($conectar,$query))) {
      $total=array();
      $total=count($_POST['enviar']);
      $id = $_POST['enviar'];            
      foreach ($id as $item) {
              include 'private/enviarMails.php'; //PHP Mailer credentials
              $mail->addAddress($item);
              $mail->Subject = $correo['mailAsunto'];
              $mail->Body    = $correo['mailMensaje'];
              if(!$mail->send()) {
                  echo 'El mail no se mandó: ' . $mail->ErrorInfo;
              } else { echo 'Se envió un correo a '.$item;}


      }
  } else { echo 'No se pudieron enviar los correos'; }
      mysqli_close($conectar);  
}
  • 写回答

1条回答 默认 最新

  • duanchendu69495 2016-08-07 12:51
    关注

    You are including your credentials in every page loop.

    Change:

    foreach ($id as $item) {
        include 'private/enviarMails.php'; //PHP Mailer credentials
        $mail->addAddress($item);
    

    To:

    include 'private/enviarMails.php'; //PHP Mailer credentials
    foreach ($id as $item) {
        $mail->addAddress($item);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办