Skip to content

Commit 4cb2a86

Browse files
committed
bug symfony#48505 [Mailer] Fix rendered templates for notifications (fabpot)
This PR was merged into the 6.2 branch. Discussion ---------- [Mailer] Fix rendered templates for notifications | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix symfony#48472 | License | MIT | Doc PR | n/a Alternative to symfony#48481 Commits ------- 085185d [Mailer] Fix rendered templates for notifications
2 parents 2b292e7 + 085185d commit 4cb2a86

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

src/Symfony/Bridge/Twig/Mime/BodyRenderer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ public function render(Message $message): void
5959

6060
if ($template = $message->getTextTemplate()) {
6161
$message->text($this->twig->render($template, $vars));
62-
$message->textTemplate(null);
6362
}
6463

6564
if ($template = $message->getHtmlTemplate()) {
6665
$message->html($this->twig->render($template, $vars));
67-
$message->htmlTemplate(null);
6866
}
6967

70-
$message->context([]);
68+
$message->markAsRendered();
7169

7270
// if text body is empty, compute one from the HTML body
7371
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {

src/Symfony/Bridge/Twig/Mime/NotificationEmail.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class NotificationEmail extends TemplatedEmail
4040
'raw' => false,
4141
'footer_text' => 'Notification e-mail sent by Symfony',
4242
];
43+
private bool $rendered = false;
4344

4445
public function __construct(Headers $headers = null, AbstractPart $body = null)
4546
{
@@ -178,6 +179,18 @@ public function getContext(): array
178179
return array_merge($this->context, parent::getContext());
179180
}
180181

182+
public function isRendered(): bool
183+
{
184+
return $this->rendered;
185+
}
186+
187+
public function markAsRendered(): void
188+
{
189+
parent::markAsRendered();
190+
191+
$this->rendered = true;
192+
}
193+
181194
public function getPreparedHeaders(): Headers
182195
{
183196
$headers = parent::getPreparedHeaders();
@@ -225,15 +238,17 @@ private function getExceptionAsString(\Throwable|FlattenException $exception): s
225238
*/
226239
public function __serialize(): array
227240
{
228-
return [$this->context, $this->theme, parent::__serialize()];
241+
return [$this->context, $this->theme, $this->rendered, parent::__serialize()];
229242
}
230243

231244
/**
232245
* @internal
233246
*/
234247
public function __unserialize(array $data): void
235248
{
236-
if (3 === \count($data)) {
249+
if (4 === \count($data)) {
250+
[$this->context, $this->theme, $this->rendered, $parentData] = $data;
251+
} elseif (3 === \count($data)) {
237252
[$this->context, $this->theme, $parentData] = $data;
238253
} else {
239254
// Backwards compatibility for deserializing data structures that were serialized without the theme

src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public function getContext(): array
6767
return $this->context;
6868
}
6969

70+
public function isRendered(): bool
71+
{
72+
return null === $this->htmlTemplate && null === $this->textTemplate;
73+
}
74+
75+
public function markAsRendered(): void
76+
{
77+
$this->textTemplate = null;
78+
$this->htmlTemplate = null;
79+
$this->context = [];
80+
}
81+
7082
/**
7183
* @internal
7284
*/

src/Symfony/Component/Mailer/Transport/AbstractTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
7676
$envelope = $event->getEnvelope();
7777
$message = $event->getMessage();
7878

79-
if ($message instanceof TemplatedEmail && ($message->getTextTemplate() || $message->getHtmlTemplate())) {
79+
if ($message instanceof TemplatedEmail && !$message->isRendered()) {
8080
throw new LogicException(sprintf('You must configure a "%s" when a "%s" instance has a text or HTML template set.', BodyRendererInterface::class, get_debug_type($message)));
8181
}
8282

src/Symfony/Component/Mailer/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"conflict": {
3434
"symfony/http-kernel": "<5.4",
3535
"symfony/messenger": "<6.2",
36-
"symfony/mime": "<6.2"
36+
"symfony/mime": "<6.2",
37+
"symfony/twig-bridge": "<6.2.1"
3738
},
3839
"autoload": {
3940
"psr-4": { "Symfony\\Component\\Mailer\\": "" },

0 commit comments

Comments
 (0)