我在循环中通过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
}
答案 0 :(得分: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.
}
}