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

Skip to content

[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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---
* Add `ExternalTemplatedEmail`

Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Add `ExternalTemplatedEmail`
* Add `ExternalTemplatedEmail`


7.0
---

Expand Down
95 changes: 95 additions & 0 deletions src/Symfony/Component/Mime/ExternalTemplatedEmail.php
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();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/**
* @return $this
*/

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);
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Mime/Tests/ExternalTemplatedEmailTest.php
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
class ExternalTemplatedEmailTest extends TestCase
final 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
}
Loading