我在循环中通过 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
}