Skip to content

[Mailer] [Sendgrid] Add template support #41714

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

Closed
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/Mailer/Bridge/Sendgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add support for templates and dynamic template data

4.4.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridApiTransport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -222,4 +223,86 @@ public function testEnvelopeSenderAndRecipients()
$this->assertCount(1, $payload['personalizations'][0]['to']);
$this->assertSame($envelopeTo, $payload['personalizations'][0]['to'][0]['email']);
}

public function testSendTemplateIdWithDynamicTemplateData()
{
$templateId = 'd-0aac27809ad64ae98d5ebaf896ea8b33';
$dynamicTemplateData = [
'foo' => 'bar',
];

$email = new Email();
$email->from(new Address('foo@example.com', 'Ms. Foo Bar'))
->to(new Address('bar@example.com', 'Mr. Recipient'))
->bcc('baz@example.com')
->text('content');

$email->getHeaders()->addTextHeader('X-Template-ID', $templateId);
$email->getHeaders()->addTextHeader('X-Dynamic-Template-Data', json_encode($dynamicTemplateData));

$response = $this->createMock(ResponseInterface::class);

$response
->expects($this->once())
->method('getStatusCode')
->willReturn(202);
$response
->expects($this->once())
->method('getHeaders')
->willReturn(['x-message-id' => '1']);

$httpClient = $this->createMock(HttpClientInterface::class);

$httpClient
->expects($this->once())
->method('request')
->with('POST', 'https://api.sendgrid.com/v3/mail/send', [
'json' => [
'personalizations' => [
[
'to' => [[
'email' => 'bar@example.com',
'name' => 'Mr. Recipient',
]],
'subject' => null,
'dynamic_template_data' => $dynamicTemplateData,
'bcc' => [['email' => 'baz@example.com']],
],
],
'from' => [
'email' => 'foo@example.com',
'name' => 'Ms. Foo Bar',
],
'content' => [
['type' => 'text/plain', 'value' => 'content'],
],
'template_id' => $templateId,
],
'auth_bearer' => 'foo',
])
->willReturn($response);

$mailer = new SendgridApiTransport('foo', $httpClient);
$mailer->send($email);
}

public function testSendWithInvalidTemplateIdThrowsInvalidArgumentException()
{
$invalidTemplateId = 'd-abcd';

$email = new Email();
$email->from(new Address('foo@example.com', 'Ms. Foo Bar'))
->to(new Address('bar@example.com', 'Mr. Recipient'))
->bcc('baz@example.com')
->text('content');

$email->getHeaders()->addTextHeader('X-Template-ID', $invalidTemplateId);

$mailer = new SendgridApiTransport('foo', $this->createMock(HttpClientInterface::class));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Invalid TemplateID. Got: "%s"', $invalidTemplateId));

$mailer->send($email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Address;
Expand Down Expand Up @@ -101,6 +102,8 @@ private function getPayload(Email $email, Envelope $envelope): array

$payload['personalizations'][] = $personalization;

$dynamicTemplateData = [];

// these headers can't be overwritten according to Sendgrid docs
// see https://sendgrid.api-docs.io/v3.0/mail-send/mail-send-errors#-Headers-Errors
$headersToBypass = ['x-sg-id', 'x-sg-eid', 'received', 'dkim-signature', 'content-transfer-encoding', 'from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'reply-to'];
Expand All @@ -109,9 +112,34 @@ private function getPayload(Email $email, Envelope $envelope): array
continue;
}

if ('x-template-id' === $name) {
$templateId = $header->getBodyAsString();

if (!preg_match('/^d\-[a-z0-9]{32}$/', $templateId)) {
throw new InvalidArgumentException(sprintf('Invalid TemplateID. Got: "%s".', $templateId));
}

$payload['template_id'] = $templateId;

continue;
}

if ('x-dynamic-template-data' === $name) {
$dynamicTemplateData = json_decode($header->getBodyAsString(), true);

continue;
}

$payload['headers'][$name] = $header->getBodyAsString();
}

if ([] !== $dynamicTemplateData) {
// we need to add the substitutions to every available personalization
foreach ($payload['personalizations'] as $key => $personalization) {
$payload['personalizations'][$key] = array_merge($payload['personalizations'][$key], ['dynamic_template_data' => $dynamicTemplateData]);
}
}

return $payload;
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy