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
-------
537c8b8aa6
[Mailer][Mailgun] Support more headers
This commit is contained in:
commit
0c22ab8526
@ -39,6 +39,11 @@ HttpFoundation
|
||||
`__construct()` instead)
|
||||
* Made the Mime component an optional dependency
|
||||
|
||||
Mailer
|
||||
------
|
||||
|
||||
* Deprecated passing Mailgun headers without their "h:" prefix.
|
||||
|
||||
Messenger
|
||||
---------
|
||||
|
||||
|
@ -1,6 +1,10 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.1.0
|
||||
|
||||
* Not prefixing headers with "h:" is deprecated.
|
||||
|
||||
4.4.0
|
||||
-----
|
||||
|
||||
|
@ -58,8 +58,17 @@ class MailgunApiTransportTest extends TestCase
|
||||
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');
|
||||
@ -69,6 +78,43 @@ class MailgunApiTransportTest extends TestCase
|
||||
|
||||
$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()
|
||||
@ -130,7 +176,7 @@ class MailgunApiTransportTest extends TestCase
|
||||
->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);
|
||||
}
|
||||
|
||||
|
@ -67,10 +67,10 @@ class MailgunApiTransport extends AbstractApiTransport
|
||||
$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']);
|
||||
@ -128,7 +128,17 @@ class MailgunApiTransport extends AbstractApiTransport
|
||||
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
|
||||
$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);
|
||||
}
|
||||
|
||||
$payload[$headerName] = $header->getBodyAsString();
|
||||
}
|
||||
|
||||
return $payload;
|
||||
|
Reference in New Issue
Block a user