Skip to content
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