0

I try to send email to fake address. And I can't catch error if this email address is not available and email can not be delivered. In SMTP-server logs I see:

Fri 2020-12-11 12:08:58.582: 03: --> 250-STARTTLS
Fri 2020-12-11 12:08:58.582: 03: --> 250 SIZE
Fri 2020-12-11 12:08:58.582: 02: <-- MAIL FROM:<my_email@mydomain.com>
Fri 2020-12-11 12:08:58.583: 03: --> 250 2.1.0 Sender OK
Fri 2020-12-11 12:08:58.583: 02: <-- RCPT TO:<somefakeaddress@mydomain.com>
Fri 2020-12-11 12:08:58.584: 03: --> 550 5.1.1 Recipient unknown <mi@f
Fri 2020-12-11 12:08:58.585: 02: <-- RSET
Fri 2020-12-11 12:08:58.585: 03: --> 250 2.0.0 RSET? Well, OK
Fri 2020-12-11 12:08:58.586: 02: <-- QUIT
Fri 2020-12-11 12:08:58.586: 03: --> 221 2.0.0 See ya in cyberspace
Fri 2020-12-11 12:08:58.586: 04: SMTP session terminated (Bytes in/out

But in my Laravel app I can't catch this error even if I use Event Listeners https://laravel.com/docs/7.x/mail#events

Of course, the code

Mail::to('somefakeaddress@mydomain.com')->send(new Mailable($mail_data));

returns nothing.

Is there any idea how to catch the error of the SMTP session in Laravel?

ArtSur
  • 59
  • 1
  • 8

1 Answers1

0

Found this solution:

Add SwiftMailer plugin to mailer

$mailer = Mail::getSwiftMailer();

$logger = new \Swift_Plugins_Loggers_ArrayLogger();

$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));

Then we can send mail as usual:

Mail::to(...)->send(new Mailable($data_for_mailable_class));

And after that we can search for any error messages in $logger array.

For test I use dump($logger);

This helped me find a solution: How to set SwifMailer plugins when using the Laravel mail class?

ArtSur
  • 59
  • 1
  • 8