Skip to content

Commit

Permalink
Mailgun: fix webhook error with null delivery-status
Browse files Browse the repository at this point in the history
Mailgun now sometimes posts `"delivery-status": null` 
in the tracking event payload. Avoid raising an AttributeError
when that occurs.

Fixes #361
  • Loading branch information
izimobil authored Mar 5, 2024
1 parent a71a0d9 commit 5949069
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ Features
(Thanks to `@Arondit`_ for the implementation.)


Fixes
~~~~~

* **Mailgun:** Avoid an error when Mailgun posts null delivery-status
to the event tracking webhook. (Thanks to `@izimobil`_ for the fix.)


v10.2
-----

Expand Down Expand Up @@ -1595,6 +1602,7 @@ Features
.. _@Flexonze: https:/Flexonze
.. _@gdvalderrama: https:/gdvalderrama
.. _@Honza-m: https:/Honza-m
.. _@izimobil: https:/izimobil
.. _@janneThoft: https:/janneThoft
.. _@jc-ee: https:/jc-ee
.. _@joshkersey: https:/joshkersey
Expand Down
8 changes: 4 additions & 4 deletions anymail/webhooks/mailgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ def esp_to_anymail_event(self, esp_event):

try:
delivery_status = event_data["delivery-status"]
except KeyError:
description = None
mta_response = None
else:
# if delivery_status is None, an AttributeError will be raised
description = delivery_status.get("description")
mta_response = delivery_status.get("message")
except (KeyError, AttributeError):
description = None
mta_response = None

if "reason" in event_data:
reject_reason = self.reject_reasons.get(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_mailgun_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,22 @@ def test_clicked_event(self):
self.assertEqual(event.event_type, "clicked")
self.assertEqual(event.click_url, "https://example.com/test")

def test_delivery_status_is_none_event(self):
raw_event = mailgun_sign_payload(
{
"event-data": {
"event": "accepted",
"delivery-status": None,
}
}
)
response = self.client.post(
"/anymail/mailgun/tracking/",
data=json.dumps(raw_event),
content_type="application/json",
)
self.assertEqual(response.status_code, 200)


@tag("mailgun")
@override_settings(ANYMAIL_MAILGUN_WEBHOOK_SIGNING_KEY=TEST_WEBHOOK_SIGNING_KEY)
Expand Down

0 comments on commit 5949069

Please sign in to comment.