lets say i have this:
$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());
}
}
}
so the exception in next loop is remembered and displayed, the output above is:
SMTP Error: The following recipients failed: some2@email: L6gvk4HSs0vIlL6gvkzbQ0 invalid destination domain <some2@email>
some3@email.com
so my question is, how do i clear the error after i process it so i can start fresh?
Because of the exception at the second address, the line with $mail->clearAddresses()
is never reached.
So your third mail wil be sent to both some2@email
and some3@email.com
, throwing the exception again.
try {
$mail->addAddress($email);
$mail->send(); // try block stops here on exception
$mail->clearAddresses(); // addresses are not cleared
} catch (Exception $e) {
// ...
}
You can call $mail->clearAddresses()
outside of the try, catch
statement.
try {
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}
$mail->clearAddresses();
You can also call $mail->clearAddresses()
before you call $mail->addAddress($email)
. That way you will always be sure the addresses are cleared before you add one.
try {
$mail->clearAddresses();
$mail->addAddress($email);
$mail->send();
} catch (Exception $e) {
// ...
}