diff --git a/src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php b/src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php index fddd9154d1..67f0c7b935 100644 --- a/src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php +++ b/src/Symfony/Component/Mailer/Bridge/Mailgun/Tests/Transport/MailgunApiTransportTest.php @@ -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']); diff --git a/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php index 76b0356f7f..33d0fb1ff5 100644 --- a/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php @@ -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;