8000 [Mailer] Support Amazon SES ConfigurationSetName by cvmiert · Pull Request #37897 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer] Support Amazo 8000 n SES ConfigurationSetName #37897

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testSend()
$this->assertSame('Hello There!', $content['Content']['Simple']['Body']['Text']['Data']);
$this->assertSame('<b>Hello There!</b>', $content['Content']['Simple']['Body']['Html']['Data']);
$this->assertSame(['replyto-1@example.com', 'replyto-2@example.com'], $content['ReplyToAddresses']);
$this->assertSame('aws-configuration-set-name', $content['ConfigurationSetName']);

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

Expand All @@ -87,6 +88,8 @@ public function testSend()
->html('<b>Hello There!</b>')
->replyTo(new Address('replyto-1@example.com'), new Address('replyto-2@example.com'));

$mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function testSend()
$this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $content['Destination_ToAddresses_member'][0]);
$this->assertSame('Fabien <fabpot@symfony.com>', $content['Source']);
$this->assertSame('Hello There!', $content['Message_Body_Text_Data']);
$this->assertSame('aws-configuration-set-name', $content['ConfigurationSetName']);

$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
<SendEmailResult>
Expand All @@ -88,6 +89,53 @@ public function testSend()
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');

$mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
}

public function testSendWithAttachments()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://email.eu-west-1.amazonaws.com:8984/', $url);
$this->assertStringContainsStringIgnoringCase('X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=ACCESS_KEY,Algorithm=HmacSHA256,Signature=', $options['headers'][0] ?? $options['request_headers'][0]);

parse_str($options['body'], $body);
$content = base64_decode($body['RawMessage_Data']);

$this->assertStringContainsString('Hello!', $content);
$this->assertStringContainsString('Saif Eddin <saif.gmati@symfony.com>', $content);
$this->assertStringContainsString('Fabien <fabpot@symfony.com>', $content);
$this->assertStringContainsString('Hello There!', $content);
$this->assertStringContainsString(base64_encode('attached data'), $content);

$this->assertSame('aws-configuration-set-name', $body['ConfigurationSetName']);

$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
<SendRawEmailResult>
<MessageId>foobar</MessageId>
</SendRawEmailResult>
</SendEmailResponse>';

return new MockResponse($xml, [
'http_code' => 200,
]);
});
$transport = new SesApiTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
$transport->setPort(8984);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!')
->attach('attached data');

$mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function testSend()
$this->assertStringContainsString('Saif Eddin <saif.gmati@symfony.com>', $content);
$this->assertStringContainsString('Fabien <fabpot@symfony.com>', $content);
$this->assertStringContainsString('Hello There!', $content);
$this->assertSame('aws-configuration-set-name', $body['ConfigurationSetName']);

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

Expand All @@ -84,6 +85,8 @@ public function testSend()
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');

$mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function testSend()
$this->assertStringContainsString('Fabien <fabpot@symfony.com>', $content);
$this->assertStringContainsString('Hello There!', $content);

$this->assertSame('aws-configuration-set-name', $body['ConfigurationSetName']);

$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
<SendRawEmailResult>
<MessageId>foobar</MessageId>
Expand All @@ -89,6 +91,8 @@ public function testSend()
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');

$mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');

$message = $transport->send($mail);

$this->assertSame('foobar', $message->getMessageId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ protected function getRequest(SentMessage $message): SendEmailRequest
if ($emails = $email->getReplyTo()) {
$request['ReplyToAddresses'] = $this->stringifyAddresses($emails);
}
if ($header = $email->getHeaders()->get('X-SES-CONFIGURATION-SET')) {
$request['ConfigurationSetName'] = $header->getBodyAsString();
}

return new SendEmailRequest($request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ private function getSignature(string $string): string
private function getPayload(Email $email, Envelope $envelope): array
{
if ($email->getAttachments()) {
return [
$payload = [
'Action' => 'SendRawEmail',
'RawMessage.Data' => base64_encode($email->toString()),
];

if ($header = $email->getHeaders()->get('X-SES-CONFIGURATION-SET')) {
$payload['ConfigurationSetName'] = $header->getBodyAsString();
}

return $payload;
}

$payload = [
Expand All @@ -118,6 +124,9 @@ private function getPayload(Email $email, Envelope $envelope): array
if ($email->getReplyTo()) {
$payload['ReplyToAddresses.member'] = $this->stringifyAddresses($email->getReplyTo());
}
if ($header = $email->getHeaders()->get('X-SES-CONFIGURATION-SET')) {
$payload['ConfigurationSetName'] = $header->getBodyAsString();
}

return $payload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\Message;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ protected function doSend(SentMessage $message): void

protected function getRequest(SentMessage $message): SendEmailRequest
{
return new SendEmailRequest([
$request = [
'Destination' => new Destination([
'ToAddresses' => $this->stringifyAddresses($message->getEnvelope()->getRecipients()),
]),
Expand All @@ -76,6 +77,13 @@ protected function getRequest(SentMessage $message): SendEmailRequest
'Data' => $message->toString(),
],
],
]);
];

if (($message->getOriginalMessage() instanceof Message)
&& $configurationSetHeader = $message->getOriginalMessage()->getHeaders()->get('X-SES-CONFIGURATION-SET')) {
$request['ConfigurationSetName'] = $configurationSetHeader->getBodyAsString();
}

return new SendEmailRequest($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractHttpTransport;
use Symfony\Component\Mime\Message;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -54,7 +55,7 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
$date = gmdate('D, d M Y H:i:s e');
$auth = sprintf('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s', $this->accessKey, $this->getSignature($date));

$response = $this->client->request('POST', 'https://'.$this->getEndpoint(), [
$request = [
'headers' => [
'X-Amzn-Authorization' => $auth,
'Date' => $date,
Expand All @@ -63,7 +64,14 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
'Action' => 'SendRawEmail',
'RawMessage.Data' => base64_encode($message->toString()),
],
]);
];

if (($message->getOriginalMessage() instanceof Message)
&& $configurationSetHeader = $message->getOriginalMessage()->getHeaders()->get('X-SES-CONFIGURATION-SET')) {
$request['body']['ConfigurationSetName'] = $configurationSetHeader->getBodyAsString();
}

$response = $this->client->request('POST', 'https://'.$this->getEndpoint(), $request);

$result = new \SimpleXMLElement($response->getContent(false));
if (200 !== $response->getStatusCode()) {
Expand Down
0