phpsymfonysymfony-messengersymfony-mailer

Symfony 6 mailer DSN with SMTP doesn't send mails


I'm trying Symfony 6 Mailer with different SMTP servers and none of them is working.

The messages are queued but not sent. I tried from different servers to discard a firewall or port issue.

There are no log messages or exceptions, so I'm quite lost.

This are some DSN I tried:

MAILER_DSN="smtp://email%40example.com:123456@host.example.com:587?encryption=tls"
MAILER_DSN="smtp://email@example.com:123456@host.example.com:587?encryption=tls"
MAILER_DSN="smtp://email@example.com:123456@host.example.com:587"

I actually tried many DSN combinations with/without encryption. I suspect the problem is in the DSN string as, if I try wrong hosts or passwords, the effect is the same.

This is a long lasting problem I haven't been able to resolve in a long time.

And this is the sending code:

use Symfony\Component\Mime\Email;

$email = (new Email())
        ->from($this->parameterBag->get('app.message.email_from'))
        ->to($to)
        ->subject($subject)
        ->text($text)
        ->html($text);

$sentMessage = $this->mailer->send($email); 

mailer.yaml content:

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

And messenger.yaml content:

framework:
    messenger:
        failure_transport: failed

        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    use_notify: true
                    check_delayed_interval: 60000
                retry_strategy:
                    max_retries: 3
                    multiplier: 2
            failed: 'doctrine://default?queue_name=failed'
            # sync: 'sync://'

        routing:
            Symfony\Component\Mailer\Messenger\SendEmailMessage: async
            Symfony\Component\Notifier\Message\ChatMessage: async
            Symfony\Component\Notifier\Message\SmsMessage: async

            # Route your messages to the transports
            # 'App\Message\YourMessage': async

Solution

  • With your current messenger configuration, emails are not sent directly, but only when messenger:consume is called.

    That's because messenger is queuing emails (or other messages such as SMS) instead of being immediately sent.

    You can learn more about the messenger component here, but if you want to ignore it for now and just send your emails synchronously by modifying your transport configuration.

    framework:
        messenger:
            transports:
                async: 'sync://'
            routing:
                Symfony\Component\Mailer\Messenger\SendEmailMessage: async
                Symfony\Component\Notifier\Message\ChatMessage: async
                Symfony\Component\Notifier\Message\SmsMessage: async
    
                # Route your messages to the transports
                # 'App\Message\YourMessage': async