Fatal error: Uncaught exception .. using php SwiftMailer
-
12-09-2019 - |
Question
I am using SwiftMailer for sending mails and if I try to use dummy email address, for example if I type in "asdf" in email address, I get this uncaught exception.
Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message
'Address in mailbox given [asdf] does not comply with RFC 2822,
I am not very experienced in OO .. so not sure how to handle this? Actually I just want it to fail if the email address is not valid but it shouldnt throw the fatal error message. Any suggestions?
Thanks.
Solution
You need to catch the exception, like this
try
{
// Your code to send the email
}
catch (Swift_RfcComplianceException $e)
{
print('Email address not valid:' . $e->getMessage());
}
This isn't an OO thing, it's an exceptions thing.
OTHER TIPS
Also, you can validate the email before sending it:
if(!Swift_Validate::email($email)){ //if email is not valid
//do something, skip them
$log_error->log($email);
}
I think that it means that the given email address doesn't respect the email adressess standards.
If the email address is valid based on what you see in the error message, make sure that there are no leading or trailing spaces in the address. eg. run trim($email_address).