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

Skip to content

Commit 58c50dc

Browse files
committed
feature #47049 [Mailer] Throw a more specific exception when a BodyRendererInterface is needed but not configured (fabpot)
This PR was merged into the 6.2 branch. Discussion ---------- [Mailer] Throw a more specific exception when a BodyRendererInterface is needed but not configured | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- 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 | n/a When a `BodyRendereInterface` is not configured, you get the following error message if sending a `TemplatedEmail`: ``` A message must have a text or an HTML part or attachments. ``` After this PR, you will get a better error message: ``` You must register a "Symfony\Component\Mime\BodyRendererInterface" when a "Symfony\Bridge\Twig\Mime\TemplatedEmail" instance has a text or HTML template set. ``` That will help debugging such issues. Commits ------- 60f49fe [Mailer] Throw a more specific exception when a BodyRendererInterface is needed by not configured
2 parents b7f4fbb + 60f49fe commit 58c50dc

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Symfony/Component/Mailer/EventListener/MessageListener.php

+6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Component\Mailer\EventListener;
1313

14+
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\Mailer\Event\MessageEvent;
1617
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
1718
use Symfony\Component\Mailer\Exception\RuntimeException;
19+
use Symfony\Component\Mailer\Exception\LogicException;
1820
use Symfony\Component\Mime\BodyRendererInterface;
1921
use Symfony\Component\Mime\Header\Headers;
2022
use Symfony\Component\Mime\Header\MailboxListHeader;
@@ -119,6 +121,10 @@ private function setHeaders(Message $message): void
119121
private function renderMessage(Message $message): void
120122
{
121123
if (!$this->renderer) {
124+
if ($message instanceof TemplatedEmail && ($message->getTextTemplate() || $message->getHtmlTemplate())) {
125+
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)));
126+
}
127+
122128
return;
123129
}
124130

0 commit comments

Comments
 (0)