phpsymfonyemailmailer

How to send email from Symfony without running consume command?


I want to send email from Symfony 6.2. I followed the documentation and made this code (gmail address is example, I'm using own domain name):

$email = (new Email())
    ->from("from@gmail.com")
    ->to("go@gmail.com")
    ->subject("Here is my subject")
    ->html("<p>Here is the content</p>");

$mailer->send($email);

I also edit the file .env.

And, actually it doesn't send email automatically, Symfony say they are queued. And it's right as it created the table with messages, put queued on them. I should use the command php bin/console messenger:consume -vv async and let it running to see email be sent.

Here is my config/packages/messenger.yaml file:

framework:
    messenger:
        failure_transport: failed
        transports:
            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'

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

How can I make email be sent from php (or at least not necessary to use the command)?


Solution

  • Symfony 6 now sends email via messenger queuing. This is a better approach, however if you would like to disable this functionality and send email directly, you can comment out (or remove) Symfony\Component\Mailer\Messenger\SendEmailMessage: async like so:

    # config/packages/messenger.yaml
    routing:
        # Symfony\Component\Mailer\Messenger\SendEmailMessage: async
        Symfony\Component\Notifier\Message\ChatMessage: async
        Symfony\Component\Notifier\Message\SmsMessage: async