Skip to content

Commit 5949069

Browse files
authored
Mailgun: fix webhook error with null delivery-status
Mailgun now sometimes posts `"delivery-status": null` in the tracking event payload. Avoid raising an AttributeError when that occurs. Fixes #361
1 parent a71a0d9 commit 5949069

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ Features
4444
(Thanks to `@Arondit`_ for the implementation.)
4545

4646

47+
Fixes
48+
~~~~~
49+
50+
* **Mailgun:** Avoid an error when Mailgun posts null delivery-status
51+
to the event tracking webhook. (Thanks to `@izimobil`_ for the fix.)
52+
53+
4754
v10.2
4855
-----
4956

@@ -1595,6 +1602,7 @@ Features
15951602
.. _@Flexonze: https://github.com/Flexonze
15961603
.. _@gdvalderrama: https://github.com/gdvalderrama
15971604
.. _@Honza-m: https://github.com/Honza-m
1605+
.. _@izimobil: https://github.com/izimobil
15981606
.. _@janneThoft: https://github.com/janneThoft
15991607
.. _@jc-ee: https://github.com/jc-ee
16001608
.. _@joshkersey: https://github.com/joshkersey

anymail/webhooks/mailgun.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ def esp_to_anymail_event(self, esp_event):
172172

173173
try:
174174
delivery_status = event_data["delivery-status"]
175-
except KeyError:
176-
description = None
177-
mta_response = None
178-
else:
175+
# if delivery_status is None, an AttributeError will be raised
179176
description = delivery_status.get("description")
180177
mta_response = delivery_status.get("message")
178+
except (KeyError, AttributeError):
179+
description = None
180+
mta_response = None
181181

182182
if "reason" in event_data:
183183
reject_reason = self.reject_reasons.get(

tests/test_mailgun_webhooks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ def test_clicked_event(self):
630630
self.assertEqual(event.event_type, "clicked")
631631
self.assertEqual(event.click_url, "https://example.com/test")
632632

633+
def test_delivery_status_is_none_event(self):
634+
raw_event = mailgun_sign_payload(
635+
{
636+
"event-data": {
637+
"event": "accepted",
638+
"delivery-status": None,
639+
}
640+
}
641+
)
642+
response = self.client.post(
643+
"/anymail/mailgun/tracking/",
644+
data=json.dumps(raw_event),
645+
content_type="application/json",
646+
)
647+
self.assertEqual(response.status_code, 200)
648+
633649

634650
@tag("mailgun")
635651
@override_settings(ANYMAIL_MAILGUN_WEBHOOK_SIGNING_KEY=TEST_WEBHOOK_SIGNING_KEY)

0 commit comments

Comments
 (0)