清除来自 PHPMailer 的最后一个异常错误以进行下一个循环
clear last exception error from PHPMailer for next loop
假设我有这个:
$emails = array('some1@email.com', 'some2@email', 'some3@email.com', 'some4@email.com');
foreach ($emails as $email) {
try {
$mail->addAddress($email);
$mail->send();
$mail->clearAddresses();
}
catch (Exception $e) {
if (strpos($e->errorMessage(), $email) > 0) {
// so here i can see if i have error like: SMTP Error: The following recipients failed: some2@email
echo $e->errorMessage();
echo "<br>";
echo $email;
}
else {
// so i would see if there is another error, like smtp login failed etc
// here unfortunately on third loop it gives me error from second loop that some2@email is nto valid even when i sent some3@email.com
die($e->errorMessage());
}
}
}
所以在下一个循环中的异常被记住并显示,上面的输出是:
SMTP Error: The following recipients failed: some2@email: L6gvk4HSs0vIlL6gvkzbQ0 invalid destination domain <some2@email>
some3@email.com
所以我的问题是,如何在处理错误后清除错误以便重新开始?
由于第二个地址的异常,$mail->clearAddresses()
的行永远不会到达。
因此您的第三封邮件将同时发送到 some2@email
和 some3@email.com
,再次抛出异常。
try {
$mail->addAddress($email);
$mail->send(); // try block stops here on exception
$mail->clearAddresses(); // addresses are not cleared
} catch (Exception $e) {
// ...
}
您可以在 try, catch
语句之外调用 $mail->clearAddresses()
。
try {
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}
$mail->clearAddresses();
您也可以在调用 $mail->addAddress($email)
之前调用 $mail->clearAddresses()
。这样一来,您将始终确保在添加地址之前清除地址。
try {
$mail->clearAddresses();
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}
假设我有这个:
$emails = array('some1@email.com', 'some2@email', 'some3@email.com', 'some4@email.com');
foreach ($emails as $email) {
try {
$mail->addAddress($email);
$mail->send();
$mail->clearAddresses();
}
catch (Exception $e) {
if (strpos($e->errorMessage(), $email) > 0) {
// so here i can see if i have error like: SMTP Error: The following recipients failed: some2@email
echo $e->errorMessage();
echo "<br>";
echo $email;
}
else {
// so i would see if there is another error, like smtp login failed etc
// here unfortunately on third loop it gives me error from second loop that some2@email is nto valid even when i sent some3@email.com
die($e->errorMessage());
}
}
}
所以在下一个循环中的异常被记住并显示,上面的输出是:
SMTP Error: The following recipients failed: some2@email: L6gvk4HSs0vIlL6gvkzbQ0 invalid destination domain <some2@email>
some3@email.com
所以我的问题是,如何在处理错误后清除错误以便重新开始?
由于第二个地址的异常,$mail->clearAddresses()
的行永远不会到达。
因此您的第三封邮件将同时发送到 some2@email
和 some3@email.com
,再次抛出异常。
try {
$mail->addAddress($email);
$mail->send(); // try block stops here on exception
$mail->clearAddresses(); // addresses are not cleared
} catch (Exception $e) {
// ...
}
您可以在 try, catch
语句之外调用 $mail->clearAddresses()
。
try {
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}
$mail->clearAddresses();
您也可以在调用 $mail->addAddress($email)
之前调用 $mail->clearAddresses()
。这样一来,您将始终确保在添加地址之前清除地址。
try {
$mail->clearAddresses();
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}