Skip to content

[Mailer] [Amazon] Add support for custom headers in ses+api #58761

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
Jan 4, 2025
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/Amazon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add support for custom headers in ses+api

7.1
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function testSend()
$this->assertSame('bounces@example.com', $content['FeedbackForwardingEmailAddress']);
$this->assertSame([['Name' => 'tagName1', 'Value' => 'tag Value1'], ['Name' => 'tagName2', 'Value' => 'tag Value2']], $content['EmailTags']);
$this->assertSame(['ContactListName' => 'TestContactList', 'TopicName' => 'TestNewsletter'], $content['ListManagementOptions']);
$this->assertSame([['Name' => 'X-Custom-Header', 'Value' => 'foobar']], $content['Content']['Simple']['Headers']);

$json = '{"MessageId": "foobar"}';

Expand All @@ -115,6 +116,7 @@ public function testSend()
$mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');
$mail->getHeaders()->addTextHeader('X-SES-SOURCE-ARN', 'aws-source-arn');
$mail->getHeaders()->addTextHeader('X-SES-LIST-MANAGEMENT-OPTIONS', 'contactListName=TestContactList;topicName=TestNewsletter');
$mail->getHeaders()->addTextHeader('X-Custom-Header', 'foobar');
$mail->getHeaders()->add(new MetadataHeader('tagName1', 'tag Value1'));
$mail->getHeaders()->add(new MetadataHeader('tagName2', 'tag Value2'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ protected function getRequest(SentMessage $message): SendEmailRequest
$request['FeedbackForwardingEmailAddress'] = $email->getReturnPath()->toString();
}

if ($customHeaders = $this->getCustomHeaders($email->getHeaders())) {
$request['Content']['Simple']['Headers'] = $customHeaders;
}

foreach ($email->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$request['EmailTags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
Expand All @@ -123,6 +127,29 @@ private function getRecipients(Email $email, Envelope $envelope): array
return array_filter($envelope->getRecipients(), fn (Address $address) => !\in_array($address, $emailRecipients, true));
}

private function getCustomHeaders(Headers $headers): array
{
$headersPrepared = [];

$headersToBypass = ['from', 'to', 'cc', 'bcc', 'return-path', 'subject', 'reply-to', 'sender', 'content-type', 'x-ses-configuration-set', 'x-ses-source-arn', 'x-ses-list-management-options'];
foreach ($headers->all() as $name => $header) {
if (\in_array($name, $headersToBypass, true)) {
continue;
}

if ($header instanceof MetadataHeader) {
continue;
}

$headersPrepared[] = [
'Name' => $header->getName(),
'Value' => $header->getBodyAsString(),
];
}

return $headersPrepared;
}

protected function stringifyAddresses(array $addresses): array
{
return array_map(fn (Address $a) => $this->stringifyAddress($a), $addresses);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mailer/Bridge/Amazon/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=8.2",
"async-aws/ses": "^1.3",
"async-aws/ses": "^1.8",
"symfony/mailer": "^7.2"
},
"require-dev": {
Expand Down
Loading