3

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.

  • I changed MESSENGER_TRANSPORT_DSN from doctrine://default?autosetup=false to doctrine://default
  • I added MAILER_DSN=smtp://from%40gmail.com:mdp@ssl0.ovh.net

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)?

Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • 1
    I believe this is the new way Symfony processes mail via messenger. To send mail without messenger, go to *config/packages/messenger.yaml* under `routing` comment out `Symfony\Component\Mailer\Messenger\SendEmailMessage: async`. – Bossman Mar 09 '23 at 11:07
  • @Bossman I just added my `config/packages/messenger.yaml` file, and the line you're giving is already without comment – Elikill58 Mar 09 '23 at 12:14
  • So have you commented it out so it is not used? You can comment it out by using `#`. Or you can remove the line altogether. – Bossman Mar 09 '23 at 15:11
  • 1
    Oh ok, sorry @Bossman I didn't understand as I'm not english. I just tried and it works, thanks ! Can you make an answer that I can accept? – Elikill58 Mar 09 '23 at 15:45

1 Answers1

2

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
Bossman
  • 1,416
  • 1
  • 11
  • 17