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

Commit 16f0c43

Browse filesBrowse files
committed
[Mailer] add support for external template engine to mail
1 parent 2276608 commit 16f0c43
Copy full SHA for 16f0c43

File tree

3 files changed

+147
-0
lines changed
Filter options

3 files changed

+147
-0
lines changed

‎src/Symfony/Component/Mime/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mime/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
* Add `ExternalTemplatedEmail`
7+
8+
49
7.0
510
---
611

+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime;
13+
14+
use Symfony\Component\Mime\Exception\LogicException;
15+
16+
class ExternalTemplatedEmail extends Email
17+
{
18+
private ?string $templateId = null;
19+
private array $context = [];
20+
private ?string $locale = null;
21+
22+
public function ensureValidity(): void
23+
{
24+
if (null === $this->getTemplateId()) {
25+
throw new LogicException('The template ID is required.');
26+
}
27+
28+
if ('1' === $this->getHeaders()->getHeaderBody('X-Unsent')) {
29+
throw new LogicException('Cannot send messages marked as "draft".');
30+
}
31+
32+
Message::ensureValidity();
33+
}
34+
35+
public function templateId(?string $templateId): static
36+
{
37+
$this->templateId = $templateId;
38+
39+
return $this;
40+
}
41+
42+
public function getTemplateId(): ?string
43+
{
44+
return $this->templateId;
45+
}
46+
47+
/**
48+
* @return $this
49+
*/
50+
public function locale(?string $locale): static
51+
{
52+
$this->locale = $locale;
53+
54+
return $this;
55+
}
56+
57+
public function getLocale(): ?string
58+
{
59+
return $this->locale;
60+
}
61+
62+
/**
63+
* @return $this
64+
*/
65+
public function context(array $context): static
66+
{
67+
$this->context = $context;
68+
69+
return $this;
70+
}
71+
72+
public function getContext(): array
73+
{
74+
return $this->context;
75+
}
76+
77+
/**
78+
* @internal
79+
*/
80+
public function __serialize(): array
81+
{
82+
return [$this->context, parent::__serialize(), $this->locale];
83+
}
84+
85+
/**
86+
* @internal
87+
*/
88+
public function __unserialize(array $data): void
89+
{
90+
[$this->context, $parentData] = $data;
91+
$this->locale = $data[2] ?? null;
92+
93+
parent::__unserialize($parentData);
94+
}
95+
}
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mime\ExternalTemplatedEmail;
16+
17+
class ExternalTemplatedEmailTest extends TestCase
18+
{
19+
public function testTemplateId()
20+
{
21+
$e = new ExternalTemplatedEmail();
22+
$e->templateId('template_id');
23+
$this->assertEquals('template_id', $e->getTemplateId());
24+
}
25+
26+
public function testContext()
27+
{
28+
$e = new ExternalTemplatedEmail();
29+
$e->context(['foo' => 'bar']);
30+
$this->assertEquals(['foo' => 'bar'], $e->getContext());
31+
}
32+
33+
public function testLocale()
34+
{
35+
$e = new ExternalTemplatedEmail();
36+
$e->locale('fr');
37+
$this->assertEquals('fr', $e->getLocale());
38+
}
39+
40+
public function testInvalidTemplateIdExternalTemplatedEmail()
41+
{
42+
$this->expectException(\LogicException::class);
43+
$this->expectExceptionMessage('The template ID is required.');
44+
45+
(new ExternalTemplatedEmail())->ensureValidity();
46+
}
47+
}

0 commit comments

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