-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer][Mime] add support for external template engine to mail #60062
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.3 | ||
--- | ||
* Add `ExternalTemplatedEmail` | ||
|
||
|
||
7.0 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,95 @@ | ||||||||||||
<?php | ||||||||||||
|
||||||||||||
/* | ||||||||||||
* This file is part of the Symfony package. | ||||||||||||
* | ||||||||||||
* (c) Fabien Potencier <[email protected]> | ||||||||||||
* | ||||||||||||
* 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(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
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); | ||||||||||||
} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the Symfony package. | ||||||
* | ||||||
* (c) Fabien Potencier <[email protected]> | ||||||
* | ||||||
* 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please assert the LogicException from the Mailer here, thanks |
||||||
$this->expectExceptionMessage('The template ID is required.'); | ||||||
|
||||||
(new ExternalTemplatedEmail())->ensureValidity(); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.