Skip to content

[Mailer] Adding support for TagHeader and MetadataHeader to the Sendgrid API transport #43018

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

Merged
merged 1 commit into from
Sep 15, 2021
Merged
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 `TagHeader` and `MetadataHeader` to the Sendgrid API transport

4.4.0
-----

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

public function testTagAndMetadataHeaders()
{
$email = new Email();
$email->getHeaders()->add(new TagHeader('category-one'));
$email->getHeaders()->add(new MetadataHeader('Color', 'blue'));
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);

$transport = new SendgridApiTransport('ACCESS_KEY');
$method = new \ReflectionMethod(SendgridApiTransport::class, 'getPayload');
$method->setAccessible(true);
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('categories', $payload);
$this->assertArrayHasKey('custom_args', $payload['personalizations'][0]);

$this->assertCount(1, $payload['categories']);
$this->assertCount(2, $payload['personalizations'][0]['custom_args']);

$this->assertSame(['category-one'], $payload['categories']);
$this->assertSame('blue', $payload['personalizations'][0]['custom_args']['Color']);
$this->assertSame('12345', $payload['personalizations'][0]['custom_args']['Client-ID']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractApiTransport;
use Symfony\Component\Mime\Address;
Expand Down Expand Up @@ -111,7 +114,8 @@ private function getPayload(Email $email, Envelope $envelope): array
$payload['reply_to'] = $emails[0];
}

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

// 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
Expand All @@ -121,9 +125,28 @@ private function getPayload(Email $email, Envelope $envelope): array
continue;
}

$payload['headers'][$name] = $header->getBodyAsString();
if ($header instanceof TagHeader) {
if (10 === \count($categories)) {
throw new TransportException(sprintf('Too many "%s" instances present in the email headers. Sendgrid does not accept more than 10 categories on an email.', TagHeader::class));
}
$categories[] = mb_substr($header->getValue(), 0, 255);
} elseif ($header instanceof MetadataHeader) {
$customArguments[$header->getKey()] = $header->getValue();
} else {
$payload['headers'][$name] = $header->getBodyAsString();
}
}

if (\count($categories) > 0) {
$payload['categories'] = $categories;
}

if (\count($customArguments) > 0) {
$personalization['custom_args'] = $customArguments;
}

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

return $payload;
}

Expand Down