Slack Alerts for Bounced Emails in Frappe
Learn how to monitor bounced emails in Frappe with SendGrid and get Slack alerts to keep communication reliable and fast.
Slack Alerts for Bounced and Undelivered Emails with Twilio SendGrid in Frappe
Monitoring email delivery is critical for ensuring your messages reach recipients. By integrating Twilio SendGrid webhooks with Frappe and Slack, you can instantly detect failed emails and alert your team.
Step 1: Create the Event Webhook in Twilio SendGrid
In the Twilio portal:
- Go to Settings → API Keys → Create API Key.
- Name it (e.g., event-webhook-manager) and give it Full Access to Mail Send / Webhooks.
- Save it securely.
Use the following cURL command to register the webhook:
curl -X POST "https://api.sendgrid.com/v3/user/webhooks/event/settings" \
--header "Authorization: Bearer $SENDGRID_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"enabled": true,
"url": "https://yourdomain.com/api/method/yourapp.api.twilio_callback",
"bounce": true,
"dropped": true,
"deferred": true,
"open": false,
"click": false,
"spam_report": true,
"unsubscribe": true,
"group_unsubscribe": true,
"group_resubscribe": true,
"friendly_name": "Email Delivery Monitor"
}'
Reference: Create an Event Webhook – Twilio Docs
Step 2: Handle Twilio Callback in Frappe
Create a Server Script (Type: API) with endpoint /api/method/yourapp.api.twilio_callback and use this code:
doc = frappe.get_doc({
"doctype": "Integration Request",
"integration_request_service": "Twilio Callback",
"data": frappe.form_dict
})
doc.insert(ignore_permissions=True)
This will store all webhook events (bounce, drop, spam, etc.) as Integration Request records.
Step 3: Slack Notification Setup
- Create a Slack channel (e.g., #email-alerts).
- Add a Slack Webhook URL record in Frappe.
- Create a Notification:
- Document Type: Integration Request
- Send Alert On: Save
- Condition:
doc.integration_request_service == "Twilio Callback"
Use this message template for the Slack alert:
{% set data = doc.data %}
{% set email = data.split("'email': '")[1].split("'")[0] if "'email': '" in data else '' %}
{% set event = data.split("'event': '")[1].split("'")[0] if "'event': '" in data else '' %}
{% set reason = data.split("'reason': '")[1].split("'")[0] if "'reason': '" in data else '' %}
{% if event in ['bounce', 'dropped', 'deferred', 'spamreport'] %}
**Email Delivery Issue Detected**
**Recipient:** {{ email }}
**Event:** {{ event }}
**Reason:** {{ reason }}
[View Email Queue](https://yourdomain.com/app/email-queue?recipient=%5B%22like%22%2C%22%25{{ email | urlencode }}%25%22%5D)
{% endif %}
Summary
With this integration:
- SendGrid webhooks capture email failures.
- Frappe server scripts log them.
- Slack alerts notify your team instantly.
No comments yet. Login to start a new discussion Start a new discussion