Skip to content

Navigation Menu

Sign in
Appearance settings

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

[Mailer] Fix rendered templates for notifications #48505

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

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions 4 src/Symfony/Bridge/Twig/Mime/BodyRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ public function render(Message $message): void

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

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

$message->context([]);
$message->markAsRendered();

// if text body is empty, compute one from the HTML body
if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
Expand Down
19 changes: 17 additions & 2 deletions 19 src/Symfony/Bridge/Twig/Mime/NotificationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NotificationEmail extends TemplatedEmail
'raw' => false,
'footer_text' => 'Notification e-mail sent by Symfony',
];
private bool $rendered = false;

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

public function isRendered(): bool
{
return $this->rendered;
}

public function markAsRendered(): void
{
parent::markAsRendered();

$this->rendered = true;
}

public function getPreparedHeaders(): Headers
{
$headers = parent::getPreparedHeaders();
Expand Down Expand Up @@ -225,15 +238,17 @@ private function getExceptionAsString(\Throwable|FlattenException $exception): s
*/
public function __serialize(): array
{
return [$this->context, $this->theme, parent::__serialize()];
return [$this->context, $this->theme, $this->rendered, parent::__serialize()];
}

/**
* @internal
*/
public function __unserialize(array $data): void
{
if (3 === \count($data)) {
if (4 === \count($data)) {
[$this->context, $this->theme, $this->rendered, $parentData] = $data;
} elseif (3 === \count($data)) {
[$this->context, $this->theme, $parentData] = $data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is worth adding a comment as done two lines below or should it be updated to cover this new case?

} else {
// Backwards compatibility for deserializing data structures that were serialized without the theme
Expand Down
12 changes: 12 additions & 0 deletions 12 src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public function getContext(): array
return $this->context;
}

public function isRendered(): bool
{
return null === $this->htmlTemplate && null === $this->textTemplate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using a boolean property as done in the other class?
Setting those to null in the next method could lead to inconsistency?

}

public function markAsRendered(): void
{
$this->textTemplate = null;
$this->htmlTemplate = null;
$this->context = [];
}

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
$envelope = $event->getEnvelope();
$message = $event->getMessage();

if ($message instanceof TemplatedEmail && ($message->getTextTemplate() || $message->getHtmlTemplate())) {
if ($message instanceof TemplatedEmail && !$message->isRendered()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior seems different here now, the error may be that the template has already been rendered.

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)));
}

Expand Down
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Mailer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"conflict": {
"symfony/http-kernel": "<5.4",
"symfony/messenger": "<6.2",
"symfony/mime": "<6.2"
"symfony/mime": "<6.2",
"symfony/twig-bridge": "<6.2.1"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\": "" },
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.