Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Mailgun API transport throws JsonException on Mailgun API error response returned as text/html #38146

Copy link
Copy link
Closed
@asprega

Description

@asprega
Issue body actions

Symfony version(s) affected: 5.1.5

Description
I'm using Symfony Mailer with Mailgun API integration. When sending an e-mail and the Mailgun API is not properly configured (e.g. wrong API key), I get this exception:

Symfony\Component\HttpClient\Exception\JsonException
"Response content-type is "text/html; charset=utf-8" while a JSON-compatible one was expected for "https://api.mailgun.net/v3/sandbox562c79333e4345ff9c02a544d6076a4d.mailgun.org/messages"."

As this exception is not caught properly, also prevents the failover mechanism to work (if configured).

How to reproduce
Just configure a wrong API key in the mailer DNS. Mailgun will reply with a text/html reponse and Forbidden as content. This will cause the exception
Similar HTML responses are obtained by using a non allowed method or by using a non existing domain.

Possible Solution
This was previously fixed with this PR: #33432 by removing the unsafe call to Response::toArray, but somehow got re-introduced (not sure in which Symfony version).

I believe this can be fixed by moving the following line inside the nested if, 2 lines below: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php#L67

On line 76, we would need to find another way to retrieve the message ID. I believe it's OK to re-parse the response as that would be anyway exclusive (one for success, another for failures).

Additional context
You can quickly verify that Mailgun replies with a content type text/html; charset=utf-8 with this cURL snippet:

curl -v --location --request POST 'https://api.mailgun.net/v3/anydomain.com/messages' --header 'Accept: application/json'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.