Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mailer] ensure only a single tag can be used with Postmark #45275

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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkApiTransport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Address;
Expand Down Expand Up @@ -147,4 +148,20 @@ public function testTagAndMetadataAndMessageStreamHeaders()
$this->assertSame(['Color' => 'blue', 'Client-ID' => '12345'], $payload['Metadata']);
$this->assertSame('broadcasts', $payload['MessageStream']);
}

public function testMultipleTagsAreNotAllowed()
{
$email = new Email();
$email->getHeaders()->add(new TagHeader('tag1'));
$email->getHeaders()->add(new TagHeader('tag2'));
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]);

$transport = new PostmarkApiTransport('ACCESS_KEY');
$method = new \ReflectionMethod(PostmarkApiTransport::class, 'getPayload');
$method->setAccessible(true);

$this->expectException(TransportException::class);

$method->invoke($transport, $email, $envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\MessageStreamHeader;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkSmtpTransport;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Email;
Expand Down Expand Up @@ -57,4 +58,19 @@ public function testTagAndMetadataAndMessageStreamHeaders()
$this->assertSame('X-PM-Metadata-Client-ID: 12345', $email->getHeaders()->get('X-PM-Metadata-Client-ID')->toString());
$this->assertSame('X-PM-Message-Stream: broadcasts', $email->getHeaders()->get('X-PM-Message-Stream')->toString());
}

public function testMultipleTagsAreNotAllowed()
{
$email = new Email();
$email->getHeaders()->add(new TagHeader('tag1'));
$email->getHeaders()->add(new TagHeader('tag2'));

$transport = new PostmarkSmtpTransport('ACCESS_KEY');
$method = new \ReflectionMethod(PostmarkSmtpTransport::class, 'addPostmarkHeaders');
$method->setAccessible(true);

$this->expectException(TransportException::class);

$method->invoke($transport, $email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mailer\SentMessage;
Expand Down Expand Up @@ -97,6 +98,10 @@ private function getPayload(Email $email, Envelope $envelope): array
}

if ($header instanceof TagHeader) {
if (isset($payload['Tag'])) {
throw new TransportException('Postmark only allows a single tag per email.');
}

$payload['Tag'] = $header->getValue();

continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mailer\SentMessage;
Expand Down Expand Up @@ -53,6 +54,10 @@ private function addPostmarkHeaders(Message $message): void

foreach ($headers->all() as $name => $header) {
if ($header instanceof TagHeader) {
if ($headers->has('X-PM-Tag')) {
throw new TransportException('Postmark only allows a single tag per email.');
}

$headers->addTextHeader('X-PM-Tag', $header->getValue());
$headers->remove($name);
}
Expand Down