Skip to content

[Mailer] Fixed Mailgun API bridge JsonException when API response is not applicaton/json #38147

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
Sep 10, 2020
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 @@ -180,6 +180,35 @@ public function testSendThrowsForErrorResponse()
$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']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
'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;
Expand Down