bug #38147 [Mailer] Fixed Mailgun API bridge JsonException when API response is not applicaton/json (asprega)
This PR was merged into the 5.1 branch.
Discussion
----------
[Mailer] Fixed Mailgun API bridge JsonException when API response is not applicaton/json
| Q | A
| ------------- | ---
| Branch? | 5.1
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix #38146
| License | MIT
| Doc PR | -
Handles gracefully cases in which the Mailgun API does not return JSON responses.
Commits
-------
ce8f7e5c02
[Mailer] Fixed Mailgun API bridge JsonException when API response is not applicaton/json
This commit is contained in:
commit
b84f3cea9a
@ -180,6 +180,35 @@ class MailgunApiTransportTest extends TestCase
|
||||
$transport->send($mail);
|
||||
}
|
||||
|
||||
public function testSendThrowsForErrorResponseWithContentTypeTextHtml()
|
||||
{
|
||||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
|
||||
$this->assertSame('POST', $method);
|
||||
$this->assertSame('https://api.mailgun.net:8984/v3/symfony/messages', $url);
|
||||
$this->assertStringContainsStringIgnoringCase('Authorization: Basic YXBpOkFDQ0VTU19LRVk=', $options['headers'][2] ?? $options['request_headers'][1]);
|
||||
|
||||
// NOTE: Mailgun API does this even if "Accept" request header value is "application/json".
|
||||
return new MockResponse('Forbidden', [
|
||||
'http_code' => 401,
|
||||
'response_headers' => [
|
||||
'content-type' => 'text/html',
|
||||
],
|
||||
]);
|
||||
});
|
||||
$transport = new MailgunApiTransport('ACCESS_KEY', 'symfony', 'us', $client);
|
||||
$transport->setPort(8984);
|
||||
|
||||
$mail = new Email();
|
||||
$mail->subject('Hello!')
|
||||
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
|
||||
->from(new Address('fabpot@symfony.com', 'Fabien'))
|
||||
->text('Hello There!');
|
||||
|
||||
$this->expectException(HttpTransportException::class);
|
||||
$this->expectExceptionMessage('Unable to send an email: Forbidden (code 401).');
|
||||
$transport->send($mail);
|
||||
}
|
||||
|
||||
public function testTagAndMetadataHeaders()
|
||||
{
|
||||
$json = json_encode(['foo' => 'bar']);
|
||||
|
@ -64,15 +64,17 @@ class MailgunApiTransport extends AbstractApiTransport
|
||||
'body' => $body->bodyToIterable(),
|
||||
]);
|
||||
|
||||
$result = $response->toArray(false);
|
||||
if (200 !== $response->getStatusCode()) {
|
||||
if ('application/json' === $response->getHeaders(false)['content-type'][0]) {
|
||||
$result = $response->toArray(false);
|
||||
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $response->getStatusCode()), $response);
|
||||
}
|
||||
|
||||
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).sprintf(' (code %d).', $response->getStatusCode()), $response);
|
||||
}
|
||||
|
||||
// The assumption here is that all 200 responses are "application/json", so it's safe to call "toArray".
|
||||
$result = $response->toArray(false);
|
||||
$sentMessage->setMessageId($result['id']);
|
||||
|
||||
return $response;
|
||||
|
Reference in New Issue
Block a user