Skip to content

Commit

Permalink
Prevent multiple notifications when email delivery fails
Browse files Browse the repository at this point in the history
SendGrid may post duplicate events to the event webhook. See
https://www.twilio.com/docs/sendgrid/for-developers/tracking-events/twilio-sendgrid-event-webhook-overview#duplicate-events

We trigger a notification when email delivery fails so if we don't handle
duplicate events then we can generate multiple notifications.
  • Loading branch information
luca-vari committed Nov 27, 2024
1 parent 0e82a45 commit 045c75d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def events_email_view(request):
if event_type == "delivered":
email.state = EmailState.DELIVERED
elif event_type in ["bounce", "dropped", "spamreport"]:
# NOTE: rudimentary handling of duplicate events.
if email.state == EmailState.DELIVERY_FAILURE:
logger.info(
"Skipping duplicate email event %s for email with sendgrid ID %s",
event_type,
sendgrid_id,
)
continue
email.state = EmailState.DELIVERY_FAILURE

email.sendgrid_id = sendgrid_id
Expand Down

0 comments on commit 045c75d

Please sign in to comment.