Is there any way to ignore error in laravel - php

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

Related

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

How do we solve error 554 5.5.1 (no valid recipients) when using PHP Swiftmailer?

When testing out our mail server we stumbled accross an error that prevents us from sending mails via PHP, though regular sending/receiving per Mail-in-a-box works without any problems.
We are running a separate Ubuntu 18.04 server that only has Mail-in-a-box with all its needed components running.
Output in the error.log text file
PHP Fatal error: Uncaught Swift_TransportException: Expected response
code 354 but got code "554", with message "554 5.5.1 Error: no valid
recipients\r\n"
PHP file
$request_email = $_POST['request-email'];
$request_name = $_POST['request-name'];
$request_text = $_POST['request-text'];
$transport = (new Swift_SmtpTransport('data.abc.xy', 587, 'tls'))
->setUsername('contact#abc.xy')
->setPassword('*******')
->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
$mailer = (new Swift_Mailer($transport));
$message = (new Swift_Message('Name: '. $request_name))
->setFrom(['' . $request_email => '' . $request_name])
->setTo(['contact#abc.xy'])
->setBody('E-Mail: ' . $request_email . $request_text)
->setContentType("text/html");
$result = $mailer->send($message);
What we have tried is to reinstall all of Mail-in-a-box and all of the components and checking everything for spelling mistakes. The ricipient does exist on our mail server and can receive and send mails manually via the client.
I have solved this error (554) just adding to laravel (.env file) these 2 lines:
MAIL_FROM_ADDRESS=you#email.com
MAIL_FROM_NAME="you#email.com"
Finally, run this command to flush mail cache config:
php artisan config:cache
The 554 5.5.1 error is the SMTP error "no valid recipients". It can occur if you've misspelled the recipient addresses but it can also occur if you are not properly authenticating to the outgoing server.
So the problem is that abc.xy is not a registered domain so you can't send an email to this address. I think it's not something related to your code.
You can catch the Swift_TransportException error and handle it in your own codebase like :
try {
$result = $mailer->send($message);
}
catch (Swift_TransportException $e) {
echo $e->getMessage();
}
I had the same problem in Laravel and fixed it by changing "from" to "replyTo".
I believe the problem is when sender's and app server's domains differs.
// in Laravel
// ...
namespace App\Notifications;
// ...
public function __construct(User $from, string $body)
{
$this->from = $from;
$this->body = $body;
}
// ...
public function toMail($notifiable)
{
$message = new MailMessage;
$message->subject('some subject');
// "no valid recipients" error:
// $message->from($this->from->email, $this->from->name_full);
// works:
// sender globally set in .env (MAIL_FROM_NAME, MAIL_FROM_ADDRESS)
$message->replyTo($this->from->email, $this->from->name_full);
$message->line(new HtmlString(nl2br($this->body)));
return $message;
}
// Swiftmailer respectively
// ...
$message = (new Swift_Message('Name: '. $request_name))
->setFrom(['contact#abc.xy' => 'contact'])
->setReplyTo([$request_email => $request_name])
// ...
I solved this issue by ensuring that the email address set in env file
MAIL_USERNAME=example#example.com
is the same as the "from" address in my custom notification mail class' toMail function
->from('example#example.com','My app name')
It seems as though some email services find it odd that the app is sending an email using one credential (env credentials) but a different email address is being masked as the sender.
A better solution would probably be to never set a "from" address in your notification classes.
One more thing is that it might be everything OK with your code, but the receiver address simply doesn't exist. We have a lot of customers from other companies and with time people simply change their job and the email address they used is deleted
"receiver email address doesn't exist anymore"
so if you try to send an email to non existing email address you will get that same error.
In my case, I got this error because I had forgotten to set the MX record on the recipient's domain, so the app didn't know where to deliver the email.
Another reason can be the domain being blacklisted, check spamhaus

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.

Fatal error: Uncaught exception .. using php SwiftMailer

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

Categories