Search code examples
phpsymfonymessengermailersymfony-messenger

Symfony - Sending Failed Messages on purpose


I'm currently using Messenger to send mails with Mailer through my MessageHandler.

Here's my messenger.yaml config file.

# config/packages/messenger.yaml
framework:
    messenger:
        transports:
            async: 
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                      table_name: messenger_message

            failed:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    queue_name: failed
                    table_name: messenger_message_failed

        routing:
            App\Message\SendEmailMessage: async

How can I send failed messages on purpose to see if my messenger_message_failed receives the data ?


Solution

  • First you need to configure the failure transport as described in the docs:

    framework:
        messenger:
            failure_transport: failed
            # ...
    

    Then in your message handler, just throw an exception:

    throw new \Exception('testing the failure transport');
    

    The message will be retried 3 times unless you disable retries, then moved to the failure transport.


    Note that when using Symfony Mailer you can natively send emails with Messenger (as described in the docs) by routing the message class to your transport:

    framework:
        messenger:
            # ...
    
            routing:
                'Symfony\Component\Mailer\Messenger\SendEmailMessage': async