From 1c9315c4688fa63032268d88dc44cc8e4f62ce65 Mon Sep 17 00:00:00 2001 From: Antony Cohen Date: Sun, 30 Mar 2025 11:52:56 +0200 Subject: [PATCH] [Mailer][Mime] add support for external template engine to mail --- src/Symfony/Component/Mime/CHANGELOG.md | 5 + .../Component/Mime/ExternalTemplatedEmail.php | 95 +++++++++++++++++++ .../Mime/Tests/ExternalTemplatedEmailTest.php | 47 +++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/Symfony/Component/Mime/ExternalTemplatedEmail.php create mode 100644 src/Symfony/Component/Mime/Tests/ExternalTemplatedEmailTest.php diff --git a/src/Symfony/Component/Mime/CHANGELOG.md b/src/Symfony/Component/Mime/CHANGELOG.md index 8d593e6ff3e33..01c58599a8e5e 100644 --- a/src/Symfony/Component/Mime/CHANGELOG.md +++ b/src/Symfony/Component/Mime/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.3 +--- + * Add `ExternalTemplatedEmail` + + 7.0 --- diff --git a/src/Symfony/Component/Mime/ExternalTemplatedEmail.php b/src/Symfony/Component/Mime/ExternalTemplatedEmail.php new file mode 100644 index 0000000000000..e0922e22f21b0 --- /dev/null +++ b/src/Symfony/Component/Mime/ExternalTemplatedEmail.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime; + +use Symfony\Component\Mime\Exception\LogicException; + +class ExternalTemplatedEmail extends Email +{ + private ?string $templateId = null; + private array $context = []; + private ?string $locale = null; + + public function ensureValidity(): void + { + if (null === $this->getTemplateId()) { + throw new LogicException('The template ID is required.'); + } + + if ('1' === $this->getHeaders()->getHeaderBody('X-Unsent')) { + throw new LogicException('Cannot send messages marked as "draft".'); + } + + Message::ensureValidity(); + } + + public function templateId(?string $templateId): static + { + $this->templateId = $templateId; + + return $this; + } + + public function getTemplateId(): ?string + { + return $this->templateId; + } + + /** + * @return $this + */ + public function locale(?string $locale): static + { + $this->locale = $locale; + + return $this; + } + + public function getLocale(): ?string + { + return $this->locale; + } + + /** + * @return $this + */ + public function context(array $context): static + { + $this->context = $context; + + return $this; + } + + public function getContext(): array + { + return $this->context; + } + + /** + * @internal + */ + public function __serialize(): array + { + return [$this->context, parent::__serialize(), $this->locale]; + } + + /** + * @internal + */ + public function __unserialize(array $data): void + { + [$this->context, $parentData] = $data; + $this->locale = $data[2] ?? null; + + parent::__unserialize($parentData); + } +} diff --git a/src/Symfony/Component/Mime/Tests/ExternalTemplatedEmailTest.php b/src/Symfony/Component/Mime/Tests/ExternalTemplatedEmailTest.php new file mode 100644 index 0000000000000..e20f81eadd85e --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/ExternalTemplatedEmailTest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\ExternalTemplatedEmail; + +class ExternalTemplatedEmailTest extends TestCase +{ + public function testTemplateId() + { + $e = new ExternalTemplatedEmail(); + $e->templateId('template_id'); + $this->assertSame('template_id', $e->getTemplateId()); + } + + public function testContext() + { + $e = new ExternalTemplatedEmail(); + $e->context(['foo' => 'bar']); + $this->assertSame(['foo' => 'bar'], $e->getContext()); + } + + public function testLocale() + { + $e = new ExternalTemplatedEmail(); + $e->locale('fr'); + $this->assertSame('fr', $e->getLocale()); + } + + public function testInvalidTemplateIdExternalTemplatedEmail() + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The template ID is required.'); + + (new ExternalTemplatedEmail())->ensureValidity(); + } +}