8000 [Mailer][Mailgun] Support more headers by Nyholm · Pull Request #36148 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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
[Mailer][Mailgun] Support more headers
  • Loading branch information
Nyholm authored and fabpot committed Mar 23, 2020
commit 537c8b8aa670c5e410be5b75409992aa7508dcf6
5 changes: 5 additions & 0 deletions UPGRADE-5.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ HttpFoundation
`__construct()` instead)
* Made the Mime component an optional dependency

Mailer
------

* Deprecated passing Mailgun headers without their "h:" prefix.

Messenger
---------

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Mailgun/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

5.1.0

* Not prefixing headers with "h:" is deprecated.

4.4.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ public function getTransportData()
public function testCustomHeader()
{
$json = json_encode(['foo' => 'bar']);
$deliveryTime = (new \DateTimeImmutable('2020-03-20 13:01:00'))->format(\DateTimeImmutable::RFC2822);

$email = new Email();
$email->getHeaders()->addTextHeader('X-Mailgun-Variables', $json);
$email->getHeaders()->addTextHeader('h:foo', 'foo-value');
$email->getHeaders()->addTextHeader('t:text', 'text-value');
$email->getHeaders()->addTextHeader('o:deliverytime', $deliveryTime);
$email->getHeaders()->addTextHeader('v:version', 'version-value');
$email->getHeaders()->addTextHeader('template', 'template-value');
$email->getHeaders()->addTextHeader('recipient-variables', 'recipient-variables-value');
$email->getHeaders()->addTextHeader('amp-html', 'amp-html-value');
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);

$transport = new MailgunApiTransport('ACCESS_KEY', 'DOMAIN');
Expand All @@ -69,6 +78,43 @@ public function testCustomHeader()

$this->assertArrayHasKey('h:x-mailgun-variables', $payload);
$this->assertEquals($json, $payload['h:x-mailgun-variables']);

$this->assertArrayHasKey('h:foo', $payload);
$this->assertEquals('foo-value', $payload['h:foo']);
$this->assertArrayHasKey('t:text', $payload);
$this->assertEquals('text-value', $payload['t:text']);
$this->assertArrayHasKey('o:deliverytime', $payload);
$this->assertEquals($deliveryTime, $payload['o:deliverytime']);
$this->assertArrayHasKey('v:version', $payload);
$this->assertEquals('version-value', $payload['v:version']);
$this->assertArrayHasKey('template', $payload);
$this->assertEquals('template-value', $payload['template']);
$this->assertArrayHasKey('recipient-variables', $payload);
$this->assertEquals('recipient-variables-value', $payload['recipient-variables']);
$this->assertArrayHasKey('amp-html', $payload);
$this->assertEquals('amp-html-value', $payload['amp-html']);
}

/**
* @legacy
*/
public function testPrefixHeaderWithH()
{
$json = json_encode(['foo' => 'bar']);
$deliveryTime = (new \DateTimeImmutable('2020-03-20 13:01:00'))->format(\DateTimeImmutable::RFC2822);

$email = new Email();
$email->getHeaders()->addTextHeader('bar', 'bar-value');

$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);

$transport = new MailgunApiTransport('ACCESS_KEY', 'DOMAIN');
$method = new \ReflectionMethod(MailgunApiTransport::class, 'getPayload');
$method->setAccessible(true);
$payload = $method->invoke($transport, $email, $envelope);

$this->assertArrayHasKey('h:bar', $payload, 'We should prefix headers with "h:" to keep BC');
$this->assertEquals('bar-value', $payload['h:bar']);
}

public function testSend()
Expand Down Expand Up @@ -130,7 +176,7 @@ public function testSendThrowsForErrorResponse()
->text('Hello There!');

$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$this->expectExceptionMessage('Unable to send an email: "i\'m a teapot" (code 418).');
$transport->send($mail);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
$result = $response->toArray(false);
if (200 !== $response->getStatusCode()) {
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $response->getStatusCode()), $response);
throw new HttpTransportException(sprintf('Unable to send an email: "%s" (code %d).', $result['message'], $response->getStatusCode()), $response);
}

throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $response->getContent(false), $response->getStatusCode()), $response);
throw new HttpTransportException(sprintf('Unable to send an email: "%s" (code %d).', $response->getContent(false), $response->getStatusCode()), $response);
}

$sentMessage->setMessageId($result['id']);
Expand Down Expand Up @@ -128,7 +128,17 @@ private function getPayload(Email $email, Envelope $envelope): array
continue;
}

$payload['h:'.$name] = $header->getBodyAsString();
// Check if it is a valid prefix or header name according to Mailgun API
$prefix = substr($name, 0, 2);
if (\in_array($prefix, ['h:', 't:', 'o:', 'v:']) || \in_array($name, ['recipient-variables', 'template', 'amp-html'])) {
$headerName = $name;
} else {
// fallback to prefix with "h:" to not break BC
8000 $headerName = 'h:'.$name;
@trigger_error(sprintf('Not prefixing the Mailgun header name with "h:" is deprecated since Symfony 5.1. Use header name "%s" instead.', $headerName), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems weird to me. The h: prefix is a mailgun specific feature. Requiring the Email object to set it means writing Mailgun-specific code in a place that may not even be aware of which transport is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nyholm I suggest removing the deprecation warning

}

$payload[$headerName] = $header->getBodyAsString();
}

return $payload;
Expand Down
0