@@ -47,6 +47,30 @@ def __init__(self, **kwargs):
4747 def build_message_payload (self , message , defaults ):
4848 return MailgunPayload (message , defaults , self )
4949
50+ def raise_for_status (self , response , payload , message ):
51+ # Mailgun issues a terse 404 for unrecognized sender domains.
52+ # Add some context:
53+ if response .status_code == 404 and "Domain not found" in response .text :
54+ raise AnymailRequestsAPIError (
55+ "Unknown sender domain {sender_domain!r}.\n "
56+ "Check the domain is verified with Mailgun, and that the ANYMAIL"
57+ " MAILGUN_API_URL setting {api_url!r} is the correct region." .format (
58+ sender_domain = payload .sender_domain , api_url = self .api_url ),
59+ email_message = message , payload = payload ,
60+ response = response , backend = self )
61+
62+ super ().raise_for_status (response , payload , message )
63+
64+ # Mailgun issues a cryptic "Mailgun Magnificent API" success response
65+ # for invalid API endpoints. Convert that to a useful error:
66+ if response .status_code == 200 and "Mailgun Magnificent API" in response .text :
67+ raise AnymailRequestsAPIError (
68+ "Invalid Mailgun API endpoint %r.\n "
69+ "Check your ANYMAIL MAILGUN_SENDER_DOMAIN"
70+ " and MAILGUN_API_URL settings." % response .url ,
71+ email_message = message , payload = payload ,
72+ response = response , backend = self )
73+
5074 def parse_recipient_status (self , response , payload , message ):
5175 # The *only* 200 response from Mailgun seems to be:
5276 # {
0 commit comments