8000 [Mailer] Implement EmailTags for Amazon Mailer by driesvints · Pull Request #45222 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer] Implement EmailTags for Amazon Mailer #45222

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
Feb 2, 2022
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
=========

6.1
---

* Add support for `X-SES-MESSAGE-TAGS`

6.0
---

Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesApiAsyncAwsTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function testSend()
$this->assertSame('aws-configuration-set-name', $content['ConfigurationSetName']);
$this->assertSame('aws-source-arn', $content['FromEmailAddressIdentityArn']);
$this->assertSame('bounces@example.com', $content['FeedbackForwardingEmailAddress']);
$this->assertSame([['Name' => 'tagName1', 'Value' => 'tag Value1'], ['Name' => 'tagName2', 'Value' => 'tag Value2']], $content['EmailTags']);

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

Expand All @@ -111,6 +113,8 @@ 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()->add(new MetadataHeader('tagName1', 'tag Value1'));
$mail->getHeaders()->add(new MetadataHeader('tagName2', 'tag Value2'));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesHttpAsyncAwsTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand Down Expand Up @@ -86,6 +87,7 @@ public function testSend()
$this->assertStringContainsString('Hello There!', $content);
$this->assertSame('aws-configuration-set-name', $body['ConfigurationSetName']);
$this->assertSame('aws-source-arn', $body['FromEmailAddressIdentityArn']);
$this->assertSame([['Name' => 'tagName1', 'Value' => 'tag Value1'], ['Name' => 'tagName2', 'Value' => 'tag Value2']], $body['EmailTags']);

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

Expand All @@ -104,6 +106,8 @@ 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()->add(new MetadataHeader('tagName1', 'tag Value1'));
$mail->getHeaders()->add(new MetadataHeader('tagName2', 'tag Value2'));

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Amazon\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesSmtpTransport;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mime\Email;

class SesSmtpTransportTest extends TestCase
{
public function testTagAndMetadataAndMessageStreamHeaders()
{
$email = new Email();
$email->getHeaders()->add(new MetadataHeader('tagName1', 'tag Value1'));
$email->getHeaders()->add(new MetadataHeader('tagName2', 'tag Value2'));

$transport = new SesSmtpTransport('user', 'pass');
$method = new \ReflectionMethod(SesSmtpTransport::class, 'addSesHeaders');
$method->setAccessible(true);
$method->invoke($transport, $email);

$this->assertCount(1, $email->getHeaders()->toArray());
$this->assertTrue($email->getHeaders()->has('X-SES-MESSAGE-TAGS'));
$this->assertSame('X-SES-MESSAGE-TAGS: tagName1=tag Value1, tagName2=tag Value2', $email->getHeaders()->get('X-SES-MESSAGE-TAGS')->toString());
}
}
< 8000 tr data-hunk="1277b309f3b749422a4ed26c729d9b11487a425ce32eecba78eeb6e4311bb55e" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use AsyncAws\Ses\ValueObject\Content;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\RuntimeException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
Expand Down Expand Up @@ -99,6 +100,12 @@ protected function getRequest(SentMessage $message): SendEmailRequest
$request['FeedbackForwardingEmailAddress'] = $email->getReturnPath()->toString();
}

foreach ($email->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$request['EmailTags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
}
}

return new SendEmailRequest($request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterf 6D40 ace;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\Message;
Expand Down Expand Up @@ -87,6 +88,13 @@ protected function getRequest(SentMessage $message): SendEmailRequest
&& $sourceArnHeader = $message->getOriginalMessage()->getHeaders()->get('X-SES-SOURCE-ARN')) {
$request['FromEmailAddressIdentityArn'] = $sourceArnHeader->getBodyAsString();
}
if ($message->getOriginalMessage() instanceof Message) {
foreach ($message->getOriginalMessage()->getHeaders()->all() as $header) {
if ($header instanceof MetadataHeader) {
$request['EmailTags'][] = ['Name' => $header->getKey(), 'Value' => $header->getValue()];
}
}
}

return new SendEmailRequest($request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@

use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;

/**
* @author Kevin Verschaeve
Expand All @@ -30,4 +35,30 @@ public function __construct(string $username, string $password, string $region =
$this->setUsername($username);
$this->setPassword($password);
}

public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
{
if ($message instanceof Message) {
$this->addSesHeaders($message);
}

return parent::send($message, $envelope);
}

private function addSesHeaders(Message $message): void
{
$metadata = [];
$headers = $message->getHeaders();

foreach ($headers->all() as $name => $header) {
if ($header instanceof MetadataHeader) {
$metadata[] = "{$header->getKey()}={$header->getValue()}";
$headers->remove($name);
}
}

if ($metadata) {
$headers->addTextHeader('X-SES-MESSAGE-TAGS', implode(', ', $metadata));
}
}
}
0