douye2110 2016-08-07 04: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 04: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部