使用phpmailer中的for循环发送多封电子邮件


sending multiple emails with for loop in phpmailer

我想发送多封内容不同的电子邮件。为此,用户需要选中复选框并单击按钮发送所有选中的电子邮件。

为此,我有一个表来显示数据库中的内容。

 select nomeUser,email,nomeVoucher,categoria,preco,confirmacao,fileName,filePDF
       from historico
       Where confirmacao = 'a confirmar'
       LIMIT $start, $per_page"; 
  $stmt = $mybd->prepare($query);  
  $stmt->execute();
  $stmt->bind_result($nomeUser,$email,$nomeVoucher,$categoria,$preco,$confirmacao,$file2,$filePDF);
             while($stmt->fetch()){ ?>
                  <tbody>
                  <tr><td><?php echo $nomeUser ?></td>
                  <td><?php echo $email ?></td>
                  <td><?php echo $nomeVoucher ?></td>
                  <td><?php echo $categoria ?></td>
                  <td><?php echo $preco ?></td>
                  <td><?php echo $confirmacao ?></td>
           <?php       $current = file_get_contents($file2,$filePDF); ?>
                  <td style='display:none;'><?php echo $current ?></td>
                  <td style='display:none;'><?php echo $file2 ?></td>
                  <td><INPUT TYPE='checkbox' NAME='mail[]' VALUE='1'></td>
                  </tr>
                  </tbody>
               <?php    }$stmt->close();

发送的代码是:

if(isset($_POST['enviar'])){
         $mails = $_POST['mail'];
            if(count($mails) > 0){
                for($i=0;$i<count($mails);$i++){
                    $fromname[$i] = "Compra do Voucher";
                    $from[$i] = "jonathan@tribanet.com";
                    $subject[$i] = "Compra do Voucher";
                    $message = "O seu pagamento foi verificado com sucesso!" . "'r'n" . 
                               "Por favor, verifica se o voucher pedido é o sucedido: " . "'r'n" .
                                    "Categoria: $categoria[$i] 'r'n" .
                                    "Nome do Voucher: $nomeVoucher[$i] 'r'n" .
                                    "Preço: $preco[$i] € 'r'n" .
                                "O seu voucher está disponível aqui $nomeVoucher[$i] 'r'r'n" .
                                "Equipa do Voucher 'r'n";   
                                    $new_array=array($email[$i]);
                                    $newstring=implode(",",$new_array);
                                    $mail = new PHPMailer();
                                    $mail->isSMTP();
                                    $mail->SMTPAuth = true;
                                    $mail->Host = "smtp.sapo.pt";
                                    $mail->Username = "jonitopsg@sapo.pt";
                                    $mail->Password = "200886";
                                    $mail->Port = 25;
                                    $mail->Sender = "$from[$i]";
                                    $mail->FromName = "$fromname[$i]";
                                    $mail->Subject = "$subject[$i]";
                                    $mail->Body = "$message";
                                    $mail->CharSet="utf-8";
                                    $mail->AddStringAttachment($current[$i],$file2[$i]);
                                    $mail->AddAddress($newstring,$nomeUser[$i]);
                                    if ($mail->send()){
                                        return true;
                                    }else 
                                        return $mail->ErrorInfo;
                                    $mail->ClearAddresses();
                                    $mail->ClearAttachments();
                                                } 
                                            }
                                    }

它显示错误:"无效地址:您必须至少提供一个收件人电子邮件地址。"我做错了什么?感谢

文档摘录:

$mail->AddAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->AddAddress('ellen@example.com');               // Name is optional

你为电子邮件收件人收到了这个:

$mail->AddAddress($newstring,$nomeUser[$i]);

看起来您可能需要切换$newstring$$nomeUser[$i]。(您确实检查了变量是否真的传递给了脚本,对吧?)