Fatal error: Uncaught exception .. using php SwiftMailer - php

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.

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.

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).

Related

How to extract delivery failure in SwiftMailer?

Docs for SwiftMailer say:
It’s possible to get a list of addresses that were rejected by the Transport by using a by-reference parameter to send(). As Swift Mailer attempts to send the message to each address given to it, if a recipient is rejected it will be added to the array.
// Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
However, I do not see a way to determinate what was the reason for this failure. E.g. send returns 0, $failures is filled with e-mail address, but I would like to know why sending has failed/rejected.
How can I do this? Is it not possible? A quick look inside sources indicate that SwiftMailer catches exceptions to fill $failedRecipients and it seems that exception's message is not saved anywhere. Am I missing something?
I don't think that it is possible to do this. Unfortunately it is only possible to see the list of email addresses that failed rather than the reasons for these addresses failing.
From the discussion on GitHub (https://github.com/swiftmailer/swiftmailer/issues/891):
Exceptions are swallowed silently in \Swift_Transport_AbstractSmtpTransport::_doMailTransaction()

Could email be sent but still throws an exception?

So I am want to send an email and record in the database that it was sent successfully, here is what I do:
First, try sending an email to the user containing the product information
Second, check if the email was sent successfully. If yes, then record in the database that it was sent successfully.
But if sending the email failed (an exception was thrown) I want to catch that exception and return an error message.
My question is:
Is there a case that the email gets sent but still throws an exception?
So by that the code returns error thinking that the email wasn't sent .. but it was actually sent and the exception was throw later after that.
// pseudo code
try{
$is_sent = send_email();
if($is_sent){
$db->email_was_sent();
}
}catch(Exception $e){
return 'Email was not sent. An exception';
}
Is there a case that the email gets sent but still throws an exception?
It depends.
If email is sent for a single recipient, any 3 of these situation could result:
email is delivered to recipient
email failed to be delivered to recipient
an exception was raised
For this case, it would be undocumented behaviour of the SwiftMailer email client
to have an email sent but still throw an exception.
If email is sent to several recipients, any 3 of these situation could result:
email is delivered to all recipients
email failed to be delivered to one or more recipient(s)
an exception was raised
For this other case, email could be delivered to some recipients and still raise an exception.
https://swiftmailer.symfony.com/docs/sending.html#using-the-send-method
AbstractSmtpTransport::send() shows that email may fail to be sent for one or more of the recipients.
https://github.com/swiftmailer/swiftmailer/blob/v6.2.1/lib/classes/Swift/Transport/AbstractSmtpTransport.php#L178

Is there any way to ignore error in laravel

Using Laravel mailable I want to send mail to my client.
But when I tried to send mail to an email that is not available or that email is not exist I got an error saying:
Expected response code 354 but got code "554", with message "554 5.5.1 Error: no valid recipients "
I tried to put try and catch method but it doesn't work.
I also find answers from the internet but i failed.
This is my try catch code:
try {
Mail::to($_val['email'])->send(new ClientMail($_val));
} catch(\Exception $e) {
// Do nothing
}
How can I ignore the error message even if the email does not exist I want to proceed

Swift mail is not catching exception for an invalid email

I'm using Swift mailer to send emails, it's not catching exception on invalid email address & throws an error.
My code is:
try
{
$message->setBody($html, "text/html");
$result = $mailer->send($message);
}
catch(Swift_RfcComplianceException $e)
{
echo "Address ".$email." seems invalid";
}
For an email address which doesn't comply with RFC it just throws this error:
Fatal error: Uncaught exception Swift_RfcComplianceException with message Address
in mailbox given [ex#example#ex.com] does not comply with RFC 2822, 3.6.2.
thrown in /swiftmailer/classes/Swift/Mime/Headers/MailboxHeader.php on line 352
Can anybody help resolving it? Simply it should catch an exception so other functions are not affected.
You're wrapping the try ... catch-block around the wrong part of your composition of the swiftmailer mail.
Excerpt from the manual:
If you add recipients automatically based on a data source that may
contain invalid email addresses, you can prevent possible exceptions
by validating the addresses using Swift_Validate::email($email) and
only adding addresses that validate. Another way would be to wrap your
setTo(), setCc() and setBcc() calls in a try-catch block and handle
the Swift_RfcComplianceException in the catch block.
Therefore you should use it while adding addresses to your Swift_Message-object, like this:
$message = Swift_Message::newInstance();
// add some message composing here...
$email = "somewrongadress.org";
try {
$message->setTo(array($email));
} catch(Swift_RfcComplianceException $e) {
echo "Address ".$email." seems invalid";
}
Additionally, I'd advise some try-catch around the $result = $mailer->send($message); too, since it might throw an exception if something else is wrong.

PHP Mailer exception "u must provide email adress.."

I'm getting an wired exception while using phpmailer v5.1
Uncaught exception 'phpmailerException' with message 'You must provide at least one recipient email address.
I checked if i have a correct Email in there
$oMail->AddReplyTo = FROM;
$oMail->AddAddress = "testmail#web.de";
$oMail->SetFrom = FROM;
but still the same exception. any ideas what the problem can be?
AddAddress is a method, not a property. So, it should be like
$oMail->AddAddress("testmail#web.de");
This applies to all three settings you're trying to apply.

Categories