Skip to content

Commit 0c22ab8

Browse files
committed
feature #36148 [Mailer][Mailgun] Support more headers (Nyholm)
This PR was squashed before being merged into the 5.1-dev branch. Discussion ---------- [Mailer][Mailgun] Support more headers | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | Fix #35776 | License | MIT | Doc PR | Im not sure if this should be classified as a bug since "setting headers are broken". Ie, you cannot use some Mailgun API features. Or, this may be a feature because now you may specify any supported custom header you want. Commits ------- 537c8b8 [Mailer][Mailgun] Support more headers
2 parents 1b90621 + 537c8b8 commit 0c22ab8

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

UPGRADE-5.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ HttpFoundation
3939
`__construct()` instead)
4040
* Made the Mime component an optional dependency
4141

42+
Mailer
43+
------
44+
45+
* Deprecated passing Mailgun headers without their "h:" prefix.
46+
4247
Messenger
4348
---------
4449

src/Symfony/Component/Mailer/Bridge/Mailgun/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
6+
* Not prefixing headers with "h:" is deprecated.
7+
48
4.4.0
59
-----
610

src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,17 @@ public function getTransportData()
5858
public function testCustomHeader()
5959
{
6060
$json = json_encode(['foo' => 'bar']);
61+
$deliveryTime = (new \DateTimeImmutable('2020-03-20 13:01:00'))->format(\DateTimeImmutable::RFC2822);
62+
6163
$email = new Email();
6264
$email->getHeaders()->addTextHeader('X-Mailgun-Variables', $json);
65+
$email->getHeaders()->addTextHeader('h:foo', 'foo-value');
66+
$email->getHeaders()->addTextHeader('t:text', 'text-value');
67+
$email->getHeaders()->addTextHeader('o:deliverytime', $deliveryTime);
68+
$email->getHeaders()->addTextHeader('v:version', 'version-value');
69+
$email->getHeaders()->addTextHeader('template', 'template-value');
70+
$email->getHeaders()->addTextHeader('recipient-variables', 'recipient-variables-value');
71+
$email->getHeaders()->addTextHeader('amp-html', 'amp-html-value');
6372
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);
6473

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

7079
$this->assertArrayHasKey('h:x-mailgun-variables', $payload);
7180
$this->assertEquals($json, $payload['h:x-mailgun-variables']);
81+
82+
$this->assertArrayHasKey('h:foo', $payload);
83+
$this->assertEquals('foo-value', $payload['h:foo']);
84+
$this->assertArrayHasKey('t:text', $payload);
85+
$this->assertEquals('text-value', $payload['t:text']);
86+
$this->assertArrayHasKey('o:deliverytime', $payload);
87+
$this->assertEquals($deliveryTime, $payload['o:deliverytime']);
88+
$this->assertArrayHasKey('v:version', $payload);
89+
$this->assertEquals('version-value', $payload['v:version']);
90+
$this->assertArrayHasKey('template', $payload);
91+
$this->assertEquals('template-value', $payload['template']);
92+
$this->assertArrayHasKey('recipient-variables', $payload);
93+
$this->assertEquals('recipient-variables-value', $payload['recipient-variables']);
94+
$this->assertArrayHasKey('amp-html', $payload);
95+
$this->assertEquals('amp-html-value', $payload['amp-html']);
96+
}
97+
98+
/**
99+
* @legacy
100+
*/
101+
public function testPrefixHeaderWithH()
102+
{
103+
$json = json_encode(['foo' => 'bar']);
104+
$deliveryTime = (new \DateTimeImmutable('2020-03-20 13:01:00'))->format(\DateTimeImmutable::RFC2822);
105+
106+
$email = new Email();
107+
$email->getHeaders()->addTextHeader('bar', 'bar-value');
108+
109+
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);
110+
111+
$transport = new MailgunApiTransport('ACCESS_KEY', 'DOMAIN');
112+
$method = new \ReflectionMethod(MailgunApiTransport::class, 'getPayload');
113+
$method->setAccessible(true);
114+
$payload = $method->invoke($transport, $email, $envelope);
115+
116+
$this->assertArrayHasKey('h:bar', $payload, 'We should prefix headers with "h:" to keep BC');
117+
$this->assertEquals('bar-value', $payload['h:bar']);
72118
}
73119

74120
public function testSend()
@@ -130,7 +176,7 @@ public function testSendThrowsForErrorResponse()
130176
->text('Hello There!');
131177

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

src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6767
$result = $response->toArray(false);
6868
if (200 !== $response->getStatusCode()) {
6969
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
70-
throw new HttpTransportException(sprintf('Unable to send an email: %s (code %s).', $result['message'], $response->getStatusCode()), $response);
70+
throw new HttpTransportException(sprintf('Unable to send an email: "%s" (code %d).', $result['message'], $response->getStatusCode()), $response);
7171
}
7272

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

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

131-
$payload['h:'.$name] = $header->getBodyAsString();
131+
// Check if it is a valid prefix or header name according to Mailgun API
132+
$prefix = substr($name, 0, 2);
133+
if (\in_array($prefix, ['h:', 't:', 'o:', 'v:']) || \in_array($name, ['recipient-variables', 'template', 'amp-html'])) {
134+
$headerName = $name;
135+
} else {
136+
// fallback to prefix with "h:" to not break BC
137+
$headerName = 'h:'.$name;
138+
@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);
139+
}
140+
141+
$payload[$headerName] = $header->getBodyAsString();
132142
}
133143

134144
return $payload;

0 commit comments

Comments
 (0)