Skip to content
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

[11.x] Add ListManagementOptions in SES mail transport #50660

Merged
merged 3 commits into from
Mar 21, 2024
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
19 changes: 19 additions & 0 deletions src/Illuminate/Mail/Transport/SesTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ protected function doSend(SentMessage $message): void
$options = $this->options;

if ($message->getOriginalMessage() instanceof Message) {
if ($listManagementOptions = $this->listManagementOptions($message)) {
$options['ListManagementOptions'] = $listManagementOptions;
}

foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
Expand Down Expand Up @@ -89,6 +93,21 @@ protected function doSend(SentMessage $message): void
$message->getOriginalMessage()->getHeaders()->addHeader('X-SES-Message-ID', $messageId);
}

/**
* Extract the SES list managenent options, if applicable.
*
* @param \Illuminate\Mail\SentMessage $message
* @return array|null
*/
protected function listManagementOptions(SentMessage $message)
{
if ($header = $message->getOriginalMessage()->getHeaders()->get('X-SES-LIST-MANAGEMENT-OPTIONS')) {
if (preg_match("/^(contactListName=)*(?<ContactListName>[^;]+)(;\s?topicName=(?<TopicName>.+))?$/ix", $header->getBodyAsString(), $listManagementOptions)) {
return array_filter($listManagementOptions, fn ($e) => in_array($e, ['ContactListName', 'TopicName']), ARRAY_FILTER_USE_KEY);
}
}
}

/**
* Get the Amazon SES client for the SesTransport instance.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Mail/Transport/SesV2Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ protected function doSend(SentMessage $message): void
$options = $this->options;

if ($message->getOriginalMessage() instanceof Message) {
if ($listManagementOptions = $this->listManagementOptions($message)) {
$options['ListManagementOptions'] = $listManagementOptions;
}

foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$options['Tags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
Expand Down Expand Up @@ -93,6 +97,21 @@ protected function doSend(SentMessage $message): void
$message->getOriginalMessage()->getHeaders()->addHeader('X-SES-Message-ID', $messageId);
}

/**
* Extract the SES list managenent options, if applicable.
*
* @param \Illuminate\Mail\SentMessage $message
* @return array|null
*/
protected function listManagementOptions(SentMessage $message)
{
if ($header = $message->getOriginalMessage()->getHeaders()->get('X-SES-LIST-MANAGEMENT-OPTIONS')) {
if (preg_match("/^(contactListName=)*(?<ContactListName>[^;]+)(;\s?topicName=(?<TopicName>.+))?$/ix", $header->getBodyAsString(), $listManagementOptions)) {
return array_filter($listManagementOptions, fn ($e) => in_array($e, ['ContactListName', 'TopicName']), ARRAY_FILTER_USE_KEY);
}
}
}

/**
* Get the Amazon SES V2 client for the SesV2Transport instance.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Mail/MailSesTransportTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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