在phpmailer中发送带有for循环的多封电子邮件

时间:2013-08-09 11:14:15

标签: php html 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();
                                                } 
                                            }
                                    }

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

1 个答案:

答案 0 :(得分:0)

摘自documentation

$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] 。 (您确实检查过变量是否实际传递给脚本,对吗?)