Skip to content

Commit

Permalink
Add ListManagementOptions in SES mail transport
Browse files Browse the repository at this point in the history
  • Loading branch information
arifszn committed Mar 19, 2024
1 parent 14c8e95 commit de305af
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/Mail/Transport/SesTransport.php
Expand Up @@ -50,6 +50,11 @@ protected function doSend(SentMessage $message): void
$options = $this->options;

if ($message->getOriginalMessage() instanceof Message) {
if ($header = $message->getOriginalMessage()->getHeaders()->get('X-SES-LIST-MANAGEMENT-OPTIONS')) {
if (preg_match("/^(contactListName=)*(?<ContactListName>[^;]+)(;\s?topicName=(?<TopicName>.+))?$/ix", $header->getBodyAsString(), $listManagementOptions)) {
$options['ListManagementOptions'] = array_filter($listManagementOptions, fn ($e) => \in_array($e, ['ContactListName', 'TopicName']), \ARRAY_FILTER_USE_KEY);
}
}
foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
Expand Down
5 changes: 5 additions & 0 deletions src/Illuminate/Mail/Transport/SesV2Transport.php
Expand Up @@ -50,6 +50,11 @@ protected function doSend(SentMessage $message): void
$options = $this->options;

if ($message->getOriginalMessage() instanceof Message) {
if ($header = $message->getOriginalMessage()->getHeaders()->get('X-SES-LIST-MANAGEMENT-OPTIONS')) {
if (preg_match("/^(contactListName=)*(?<ContactListName>[^;]+)(;\s?topicName=(?<TopicName>.+))?$/ix", $header->getBodyAsString(), $listManagementOptions)) {
$options['ListManagementOptions'] = array_filter($listManagementOptions, fn ($e) => \in_array($e, ['ContactListName', 'TopicName']), \ARRAY_FILTER_USE_KEY);
}
}
foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
Expand Down
2 changes: 2 additions & 0 deletions tests/Mail/MailSesTransportTest.php
Expand Up @@ -62,6 +62,7 @@ public function testSend()
$message->bcc('you@example.com');
$message->replyTo(new Address('taylor@example.com', 'Taylor Otwell'));
$message->getHeaders()->add(new MetadataHeader('FooTag', 'TagValue'));
$message->getHeaders()->addTextHeader('X-SES-LIST-MANAGEMENT-OPTIONS', 'contactListName=TestList;topicName=TestTopic');

$client = m::mock(SesClient::class);
$sesResult = m::mock();
Expand All @@ -73,6 +74,7 @@ public function testSend()
->with(m::on(function ($arg) {
return $arg['Source'] === 'myself@example.com' &&
$arg['Destinations'] === ['me@example.com', 'you@example.com'] &&
$arg['ListManagementOptions'] === ['ContactListName' => 'TestList', 'TopicName' => 'TestTopic'] &&
$arg['Tags'] === [['Name' => 'FooTag', 'Value' => 'TagValue']] &&
strpos($arg['RawMessage']['Data'], 'Reply-To: Taylor Otwell <taylor@example.com>') !== false;
}))
Expand Down
2 changes: 2 additions & 0 deletions tests/Mail/MailSesV2TransportTest.php
Expand Up @@ -62,6 +62,7 @@ public function testSend()
$message->bcc('you@example.com');
$message->replyTo(new Address('taylor@example.com', 'Taylor Otwell'));
$message->getHeaders()->add(new MetadataHeader('FooTag', 'TagValue'));
$message->getHeaders()->addTextHeader('X-SES-LIST-MANAGEMENT-OPTIONS', 'contactListName=TestList;topicName=TestTopic');

$client = m::mock(SesV2Client::class);
$sesResult = m::mock();
Expand All @@ -73,6 +74,7 @@ public function testSend()
->with(m::on(function ($arg) {
return $arg['Source'] === 'myself@example.com' &&
$arg['Destination']['ToAddresses'] === ['me@example.com', 'you@example.com'] &&
$arg['ListManagementOptions'] === ['ContactListName' => 'TestList', 'TopicName' => 'TestTopic'] &&
$arg['Tags'] === [['Name' => 'FooTag', 'Value' => 'TagValue']] &&
strpos($arg['Content']['Raw']['Data'], 'Reply-To: Taylor Otwell <taylor@example.com>') !== false;
}))
Expand Down

0 comments on commit de305af

Please sign in to comment.