Search code examples
phpsymfonyemailmailersymfony5

Consuming emails async in Symfony 5


I use Symfony 5 messanger to handle emails sending in async way. I have:

  • email DNS configured in .env

    MAILER_DSN=smtp://localhost

  • messanger transport DNS in .env (I use database to store emails queue)

    MESSENGER_TRANSPORT_DSN=doctrine://default

  • transports and routing in messenger.yaml

      serializer:
          default_serializer: messenger.transport.symfony_serializer
    
      transports:
          # https://symfony.com/doc/current/messenger.html#transport-configuration
          async: '%env(MESSENGER_TRANSPORT_DSN)%'
          failed: 'doctrine://default?queue_name=failed'
    
          # sync: 'sync://'
    
      routing:
          # Route your messages to the transports
          # 'App\Message\YourMessage': async
          'Symfony\Component\Mailer\Messenger\SendEmailMessage':  async
    

Current situation: sending emails to queue works - using mailer results in inserting record in database table messanger_messages.

Problem: If I understood docs correctly, for async emails processing, first emails are queued into DB (for my case) and they are then sent one by one with command php bin/console messenger:consume -vv async. Unfortunatelly executing this command does not result in emails being sent. Records in database remain the same, they do not fall into failed category and no emails are generated.

To be sure that sending configuration is correct I switched to sync method and in this case emails are immediately generated and sent. So my question is wheter there is another step which should be done to make queued emails to be consumed eg. writting custom sending handler (I assumed that messanger:consume command will do this for me) or if there is any other part of this process I missed which causes that queued emails are not consumed.


Solution

  • I figured it out so adding answer for posterity. Command messanger:consume was not working due to an issue in server configuration.

    Messages inserted in database were inserted using current datetime based on timezone set in apache php.ini. As it happens php has second, different php.ini configuration file applicable to CLI commands only.

    Executing messanger:consume in CLI made script to use different timezone and thus causing query used to fetch messages in queue to return empty result (conditions could never be met).

    Updating and synchronizing timezones in both php.ini files solved the issue and made mechanism to consume messages from queue.

    You can locate console php.ini using php --ini in CLI.