0

我在循环中通过 PHPMailer 发送电子邮件,但如果由于某种原因我发送到接收服务器不接受的地址,PHPMailer 会引发异常并且脚本死亡。我希望我的脚本继续并以另一种方式处理这个错误的地址。

关于如何避免 PHPMailer 死于异常的任何想法?

try
{
    // prep
    $mail->IsSMTP();
    $mail->SMTPKeepAlive = true;
    $mail->SMTPDebug = 0;
    $mail->SetFrom(email_reply_adress, email_from_name);
    $mail->Host = email_smtp_host;
    $mail->SMTPAuth = true; 
    $mail->Username = email_username; 
    $mail->Password = email_password;
    $mail->IsHTML(true);
    $mail->AddReplyTo(email_reply_adress, email_from_name);

    //
    $query = "SELECT * FROM table WHERE sent = '0'";
    $result = @mysql_query($query);
    while($row = @mysql_fetch_array($result))
    {
        // receiver
        $mail->AddAddress($row['email']);

        // subject
        $mail->Subject = utf8_decode($row['subject']);

        // build mail
        $mail->Body = utf8_decode(stripslashes($row['content']));

        // send it                  
        if($mail->Send())
        {
            // stats
            $sent = "UPDATE table SET sentdate = now(), sent = '1' WHERE id = '".$row['id']."'";
            mysql_query($sent);
        }

        // clean up lists
        $mail->ClearAllRecipients();
    }
}
catch(phpmailerException $e)
{
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
4

1 回答 1

0

您可能希望将引发异常的代码放入 try..catch 块中...对您的代码的外观进行大量猜测,但这应该会给您一个想法。

foreach($emails as $email){
      try{
          // do your work
      } catch (Exception $e){
          echo $email." not sent .. Error = ".$e->getMessage()."\n";
          // perform logging and so other meaningful stuff here. 
      }
}
于 2013-07-29T19:43:51.627 回答