Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Templated email messages fails when sending async #39190

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
ronnylt opened this issue Nov 27, 2020 · 8 comments
Closed

Templated email messages fails when sending async #39190

ronnylt opened this issue Nov 27, 2020 · 8 comments

Comments

@ronnylt
Copy link
Contributor

ronnylt commented Nov 27, 2020

Symfony version(s) affected: 5.x

Description
When sending a TemplatedEmail asynchronously, an exception is thrown : Symfony\Component\Mime\Exception\LogicException: A message must have a text or an HTML part or attachments..

The rendering of the templated is not done, as it's deferred to the actual email sending, but when the messenger's message is created it fails:

1) Symfony\Bundle\FrameworkBundle\Tests\Functional\MailerTest::testSendingQueuedMessage
Symfony\Component\Mime\Exception\LogicException: A message must have a text or an HTML part or attachments.

/symfony/src/Symfony/Component/Mime/Email.php:405
/symfony/src/Symfony/Component/Mailer/SentMessage.php:33
/symfony/src/Symfony/Component/Mailer/Transport/AbstractTransport.php:67
/symfony/src/Symfony/Component/Mailer/Mailer.php:42
/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php:132

How to reproduce
With a the following test, or by sending async templated messages as documented here: https://symfony.com/doc/current/mailer.html#sending-messages-async


class MailerTest extends AbstractWebTestCase
{
    public function testSendingQueuedMessage()
    {
        self::bootKernel(['test_case' => 'Mailer']);

        $testTransport = new class() extends AbstractTransport {
            public function __toString(): string
            {
                return 'dummy://local';
            }

            protected function doSend(SentMessage $message): void
            {
            }
        };

        $mailer = new Mailer($testTransport);

            $message = (new TemplatedEmail())
            ->subject('Test subject')
            ->from('[email protected]')
            ->to('[email protected]')
            ->htmlTemplate('template.html.twig')
        ;

        $mailer->send($message);
    }
}
@ronnylt
Copy link
Contributor Author

ronnylt commented Nov 27, 2020

What is the intended behaviour when queueing template messages, doing the rendering before enqueuing the message for async sending, OR just before actually sending the message to the mailer transport?

In other words, is rendering intended to be done before dispatching to the messenger's bus?

@ronnylt ronnylt closed this as completed Dec 11, 2020
@ronnylt ronnylt reopened this Dec 11, 2020
@stof
Copy link
Member

stof commented Dec 11, 2020

@ronnylt your test is created its own mailer without wiring the event dispatcher with the BodyRendererListener. So your test mailer does not configure support for templates. You should be using the Mailer service defined by FrameworkBundle with the full config

@ronnylt
Copy link
Contributor Author

ronnylt commented Dec 11, 2020

@stof here is a PR with a failing test demonstrating the issue described: #39458

@mburtscher
Copy link

I am currently trying to implement templated emails in an application using Symfony components w/o the framework and I am experiencing the same bug.

Looking at the code in Mailer.php: Mailer is throwing the MessageEvent which MessageListener will be listening to to render the body. However it dispatches it using a $clonedMessage which will not be used for the SendEmailMessage dispatched on the message bus. There the original $message is used which contains no rendered body.

The bug was introduced by commit fc4be48 trying to fix duplicate events being dispatched due to commit 829566c.

I am not 100% into the Symfony architecture and configuration but I wonder how anyone is still able to send templated emails since their email will never be dispatched on the message bus with the rendered body.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Just a quick reminder to make a comment on this. If I don't hear anything I'll close this.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

@fabpot
Copy link
Member

fabpot commented Jul 24, 2022

See #47049 where we now throw a better error message to help debugging such errors.

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