0

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('foo@example.com')
            ->to('bar@example.com')
            ->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('foo@example.com')
            ->to('bar@example.com')
            ->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.
Benni
  • 1,023
  • 11
  • 15
  • 1
    Please show the code you are using to inject and use the mailer interface. Generally speaking, Symfony uses the framework extension to create the [`mailer.transports` as a chain of transports](https://github.com/symfony/framework-bundle/blob/6.0/DependencyInjection/FrameworkExtension.php#L2319) that should contain the `TransportInterface` that acts as a factory for the provided DSN, which you should be able to see by running `php bin/console debug:container --show-arguments mailer.default_transport`. – Will B. Mar 16 '22 at 14:12
  • 1
    Also please update your question to show the results of running the `php bin/console debug:config framework`. I have a suspicion that something is occurring with your configuration that the mailer is not failing silently but doing something else, unless you have evidence indicating otherwise you should include it in your question as it may point to what is happening. – Will B. Mar 16 '22 at 14:20
  • When you run `php bin/console debug:container --show-arguments mailer.transports` you should see `Arguments Array (1 element(s))` which would be `mailer.default_transport`. In your container file, `var/cache//Container***/src***Container.php`, what does the `Container***::getMailer_TransportsService()` and `Container***::getMailerService()` methods look like? Assuming they are correct, try using the `__construct(TransportInterface $transport)` directly and debug it with `$sent = $this->transport->send($email);` using `dd($sent->getDebug());` and show us what it says. – Will B. Mar 17 '22 at 14:43

1 Answers1

1

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.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '22 at 01:37
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31893617) – MD. RAKIB HASAN Jun 02 '22 at 05:12