Symfony doesn't autowire Sendmail Transport from MAILER_DSN

1.5k Views Asked by At

I want to use Symfony Mailer in a Symfony 6 application. I have configured following files, like explained the docs.

// .env

MAILER_DSN=sendmail://default?command=/usr/bin/sendmail%20-t
// .config/packages/mailer.yaml

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

This is the service class where I want to use the Mailer (reduced example):

<?php

namespace App\Service;

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

class MyService
{
    private $mailer;

    public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
    }
    
    public function sendMail()
    {
        $email = (new Email())
            ->from('[email protected]')
            ->to('[email protected]')
            ->subject('Testsubject')
            ->text('Lorem ipsum')
        ;
        $this->mailer->send($email);
    }
}

However, sending an email fails silently without any exception thrown. When I put a debug output in the Mailer's constructor, I see that it recieves an instance of Symfony\Component\Mailer\Transport\Transports as transport.

When I instanciate the Mailer myself, using the same DSN string:

use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;

//[...]

    public function sendMailAlternative()
    {
        $email = (new Email())
            ->from('[email protected]')
            ->to('[email protected]')
            ->subject('Testsubject')
            ->text('Lorem ipsum')
        ;
        $mailer = new Mailer(Transport::fromDsn('sendmail://default?command=/usr/bin/sendmail%20-t'));
        $mailer->send($email);
    }

This way, everything works fine and the email is sent. Debugging the Mailer's constructor shows that this time it recieves an instance of Symfony\Component\Mailer\Transport\SendmailTransport.

Shouldn't Symfony autowire this instance of SendmailTransport when I provide sendmail://default... as MAILER_DSN ?

EDIT: As requested by @Will B., here is the mailer section of the bin/console config:debug framework command output...

[...]
    mailer:
        dsn: '%env(MAILER_DSN)%'
        enabled: true
        message_bus: null
        transports: {  }
        headers: {  }
[...]

... and also the output of bin/console debug:container --show-arguments mailer.default_transport:

Information for Service "mailer.default_transport"
==================================================

 Interface for all mailer transports.

 ----------------- -------------------------------------------------------
  Option            Value
 ----------------- -------------------------------------------------------
  Service ID        mailer.default_transport
  Class             Symfony\Component\Mailer\Transport\TransportInterface
  Tags              -
  Public            no
  Synthetic         no
  Lazy              no
  Shared            yes
  Abstract          no
  Autowired         no
  Autoconfigured    no
  Factory Service   mailer.transport_factory
  Factory Method    fromString
  Arguments         %env(MAILER_DSN)%
 ----------------- -------------------------------------------------------


 ! [NOTE] The "mailer.default_transport" service or alias has been removed or inlined when the container was compiled.
1

There are 1 best solutions below

2
On

For the ones that have the same issue, the problem might be the fact that emails are sent asynchronously, this was my case.

I just forced emails to be sent synchronously which was not the case.