Skip to content

[Mailer] \Symfony\Component\Mailer\Event\MessageEvent never consumed #31449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Perf opened this issue May 9, 2019 · 4 comments
Closed

[Mailer] \Symfony\Component\Mailer\Event\MessageEvent never consumed #31449

Perf opened this issue May 9, 2019 · 4 comments

Comments

@Perf
Copy link

Perf commented May 9, 2019

Symfony version(s) affected: 4.3.0-BETA1

Description
When sending email using a new symfony/mailer component an event \Symfony\Component\Mailer\Event\MessageEvent us dispatched.
Mailer component has two subscribers for this event:
\Symfony\Component\Mailer\EventListener\MessageListener
\Symfony\Component\Mailer\EventListener\EnvelopeListener
but non of them are fired in case of an event.

Command bin/console de:ev also doesn't shows these listeners.

How to reproduce

  1. create one simple twig template for html part of email, for example:
# templates/email/hello.html.twig
<p>Hi {{ email.to }}!</p>
  1. create a console command and inject Mailer in constructor
    private $mailer;

    public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
        parent::__construct();
    }
  1. put in execute method something like:
        $email = new TemplatedEmail();
        $email
            ->from('no-reply@domain.com')
            ->to('me@domain.com')
            ->subject('Test email message')
            ->htmlTemplate('email/hello.html.twig')
            ;

        $this->mailer->send($email);
  1. get an Exception

In HandleMessageMiddleware.php line 82:
A message must have a text and/or an HTML part.
In Email.php line 427:
A message must have a text and/or an HTML part.

This exception raised because \Symfony\Component\Mailer\EventListener\MessageListener was not fired.

Possible Solution
¯\_(ツ)_/¯

Additional context
symfony/messenger is installed with default configuration
symfony/swiftmailer-bundle is not installed

@Perf
Copy link
Author

Perf commented May 9, 2019

Update:
after adding following lines in services.yaml

    Symfony\Component\Mailer\EventListener\MessageListener:
        tags: ['kernel.event_subscriber']

    Symfony\Component\Mailer\EventListener\EnvelopeListener:
        tags: ['kernel.event_subscriber']

For event MessageEvent both subscribers are fired to process it, BUT when MessageListener is created, an instance of BodyRenderer is not injected.

@Perf
Copy link
Author

Perf commented May 9, 2019

Update2:
explicitly configuring $renderer argument helps... but all this issue looks like services autoconfigure and autowire is not properly working, I guess.

    Symfony\Component\Mailer\EventListener\MessageListener:
        tags: ['kernel.event_subscriber']
        arguments:
            $renderer: '@twig.mime_body_renderer'

@fabpot
Copy link
Member

fabpot commented May 10, 2019

@Perf fixed in #31458

fabpot added a commit that referenced this issue May 10, 2019
This PR was merged into the 4.3 branch.

Discussion
----------

[TwigBundle] Fix Mailer integration in Twig

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31449
| License       | MIT
| Doc PR        | n/a

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

53b68af [TwigBundle] fixed Mailer integration in Twig
@fabpot fabpot closed this as completed May 10, 2019
@bellu
Copy link

bellu commented Jul 7, 2022

I'm using 5.4 LTS and this event is never fired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants