Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osxtra's avatar
Level 1

Gather Symfony Mailer Response Code and Message

Hi, All,

More of a Symfony question than Laravel, but am curious how to get the response code and message when sending emails via SMTP.

I know you can test for an UnexpectedResponseException, but both the "code" and "message" properties of that object are private so you can't pull them separately.

Plus, that'd be the wrong place to look unless sending the message actually failed.

Many google searches have titles like "Expected Response 250" etc., but none of them actually talk about how to simply capture the response.

There's also a post here: https://laracasts.com/discuss/channels/laravel/symfonycomponentmailerexceptiontransportexception-expected-response-code-250-but-got-empty-code but again that's talking about exceptions, and doesn't mention simply grabbing the code and message.

I was able to convert the exception object to a string, read it in as an array, then parse the first few lines, but that's a really ugly way to go about it, and still only works if the send fails.

How would one gather just the response code and message from a sent email, whether or not there was an exception?

Have poked around Symfony's mailer classes, docs and website, but not seen a simple way to accomplish this.

Any replies most appreciated. Thanks!

0 likes
2 replies
LaryAI's avatar
Level 58

To gather the response code and message when sending emails using Symfony Mailer, you can utilize the Symfony\Component\Mailer\Event\MessageEvent and Symfony\Component\Mailer\EventListener\MessageLoggerListener. These components allow you to listen to events and capture the response details.

Here's a step-by-step guide on how to achieve this:

  1. Register the Event Listener:

    First, you need to register an event listener that will listen to the MessageEvent. This can be done in your Symfony service configuration.

  2. Create a Custom Event Listener:

    Create a custom event listener class that will handle the MessageEvent and extract the response details.

    namespace App\EventListener;
    
    use Symfony\Component\Mailer\Event\MessageEvent;
    use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
    use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
    
    class MailerResponseListener
    {
        public function onMessageEvent(MessageEvent $event)
        {
            $transport = $event->getTransport();
    
            if ($transport instanceof EsmtpTransport) {
                $lastResponse = $transport->getLastResponse();
    
                if ($lastResponse) {
                    $responseCode = $lastResponse->getCode();
                    $responseMessage = $lastResponse->getMessage();
    
                    // Log or handle the response code and message as needed
                    // For example, you can log them:
                    // $this->logger->info('SMTP Response', ['code' => $responseCode, 'message' => $responseMessage]);
                }
            }
        }
    }
    
  3. Register the Listener in Services Configuration:

    Register your custom listener in the services.yaml or equivalent configuration file.

    services:
        App\EventListener\MailerResponseListener:
            tags:
                - { name: 'kernel.event_listener', event: 'mailer.message_sent', method: 'onMessageEvent' }
    
  4. Use the Listener:

    With the listener registered, it will automatically capture the response code and message whenever an email is sent using the Symfony Mailer.

This setup allows you to capture the SMTP response code and message for each email sent, regardless of whether an exception occurs. You can then log or handle these details as needed in your application.

osxtra's avatar
Level 1

'k, pretty dumb mistake, the get_code() and get_message() methods worked, was trying to access the protected properties directly.

But that still only helps for failed messages.

Need to learn more about events. The bot's answer produced no output. It also didn't specify creating an event, but thought maybe Symfony had its own event code running and all I really needed was the listener. Nothing ever fired in the listener though. Also, seems the namespace of the listener should be App\Listeners - App\EventListeners causes event:cache to fail.

Please or to participate in this conversation.