admin管理员组

文章数量:1355665

We are looking for a code snipped to test a Laravel mail SMTP while printing out the SMTP dialog. The SMTP dialog usually contains the message ID, so in case a mail does not reach it can be traced on the involved server.

In previous Laravel versions we used

$to = '[email protected]';

Mail::getSwiftMailer()->registerPlugin(   new Swift_Plugins_LoggerPlugin( new Swift_Plugins_Loggers_EchoLogger(false) ));

Mail::raw('Testmail', function ($message) use ($to) {  $message->to($to)->subject('Testmail'); });

This leads to

 BadMethodCallException  Method Illuminate\Mail\Mailer::getSwiftMailer does not exist.

How can this be ported to Laravel 11 which uses Symfony mailer?

Tried a bit with ChatGPT but no luck so far.

We are looking for a code snipped to test a Laravel mail SMTP while printing out the SMTP dialog. The SMTP dialog usually contains the message ID, so in case a mail does not reach it can be traced on the involved server.

In previous Laravel versions we used

$to = '[email protected]';

Mail::getSwiftMailer()->registerPlugin(   new Swift_Plugins_LoggerPlugin( new Swift_Plugins_Loggers_EchoLogger(false) ));

Mail::raw('Testmail', function ($message) use ($to) {  $message->to($to)->subject('Testmail'); });

This leads to

 BadMethodCallException  Method Illuminate\Mail\Mailer::getSwiftMailer does not exist.

How can this be ported to Laravel 11 which uses Symfony mailer?

Tried a bit with ChatGPT but no luck so far.

Share Improve this question edited Mar 28 at 9:57 Alex asked Mar 28 at 9:43 AlexAlex 35.3k19 gold badges114 silver badges197 bronze badges 4
  • This might not be possible so easy anymore ... related GitHub issue: github/symfony/symfony/issues/37570 – Alex Commented Mar 28 at 9:49
  • 1 What do you want to achieve? Symfony throws a lot of events, why not catch any of them? For example, SentMessageEvent could help – Nico Haase Commented Mar 28 at 9:55
  • mostly seeing the SMTP Dialog. refined the question – Alex Commented Mar 28 at 9:57
  • 1 Did you check what getDebug() from the SendMessage object has to offer? – Nico Haase Commented Mar 28 at 10:02
Add a comment  | 

2 Answers 2

Reset to default 2

Based on Nico's answer, I came up with the following snipped which can easily be pasted to php artisan tinker to debug mail delivery of a Laravel 9+ appication:

$to = '[email protected]';

\Illuminate\Support\Facades\Event::listen(function (\Illuminate\Mail\Events\MessageSent $event) {
    echo "Message-ID\n";
    echo $event->sent->getSymfonySentMessage()->getMessageId() . "\n";
    echo "SMTP Dialog:\n";
    echo $event->sent->getSymfonySentMessage()->getDebug() . "\n";
});
\Illuminate\Support\Facades\Mail::raw('Testmail', function ($message) use ($to) {
    $message->to($to)->subject('Testmail ' . date('Y-m-d H:i:s'));
});

You can simply achieve that with symfony/mailer: Write an event handler that listens for SentMessageEvent. It contains an object of SentMessage, and its property debug contains the full smtp dialog.

本文标签: symfonyDebug Laravel Mail Sending in Laravel 11 including SMTP DialogStack Overflow