Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7940cc4

Browse filesBrowse files
committed
feature #47075 [Mime] Change the way we avoid rendering an email twice (fabpot)
This PR was merged into the 6.2 branch. Discussion ---------- [Mime] Change the way we avoid rendering an email twice | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes-ish | New feature? | yes-ish <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#17065 Trying to kill several birds with one stone here. I was not very happy with the way we ensured that emails were not renderer more than once. This solution seems easier and allows for people to render an email **before** sending it to Messenger (important when you have Doctrine entities in the context for instance) as documented in the related doc PR. Commits ------- cc3b871 [Mime] Change the way we avoid rendering an email twice
2 parents 41dfb72 + cc3b871 commit 7940cc4
Copy full SHA for 7940cc4

File tree

1 file changed

+8
-24
lines changed
Filter options

1 file changed

+8
-24
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Mime/BodyRenderer.php
+8-24
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ public function render(Message $message): void
4545
return;
4646
}
4747

48-
$messageContext = $message->getContext();
49-
50-
$previousRenderingKey = $messageContext[__CLASS__] ?? null;
51-
unset($messageContext[__CLASS__]);
52-
$currentRenderingKey = $this->getFingerPrint($message);
53-
if ($previousRenderingKey === $currentRenderingKey) {
48+
if (null === $message->getTextTemplate() && null === $message->getHtmlTemplate()) {
49+
// email has already been rendered
5450
return;
5551
}
5652

53+
$messageContext = $message->getContext();
54+
5755
if (isset($messageContext['email'])) {
5856
throw new InvalidArgumentException(sprintf('A "%s" context cannot have an "email" entry as this is a reserved variable.', get_debug_type($message)));
5957
}
@@ -64,34 +62,20 @@ public function render(Message $message): void
6462

6563
if ($template = $message->getTextTemplate()) {
6664
$message->text($this->twig->render($template, $vars));
65+
$message->textTemplate(null);
6766
}
6867

6968
if ($template = $message->getHtmlTemplate()) {
7069
$message->html($this->twig->render($template, $vars));
70+
$message->htmlTemplate(null);
7171
}
7272

73+
$message->context([]);
74+
7375
// if text body is empty, compute one from the HTML body
7476
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
7577
$message->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
7678
}
77-
$message->context($message->getContext() + [__CLASS__ => $currentRenderingKey]);
78-
}
79-
80-
private function getFingerPrint(TemplatedEmail $message): string
81-
{
82-
$messageContext = $message->getContext();
83-
unset($messageContext[__CLASS__]);
84-
85-
$payload = [$messageContext, $message->getTextTemplate(), $message->getHtmlTemplate()];
86-
try {
87-
$serialized = serialize($payload);
88-
} catch (\Exception) {
89-
// Serialization of 'Closure' is not allowed
90-
// Happens when context contain a closure, in that case, we assume that context always change.
91-
$serialized = random_bytes(8);
92-
}
93-
94-
return md5($serialized);
9579
}
9680

9781
private function convertHtmlToText(string $html): string

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.