Prompt
How do I add email webhooks so my app knows when something bounces or gets opened?
Latest observation
To get your app notified when emails bounce or are opened, you typically set up email provider webhooks (also called event notifications). The exact steps depend on the email service you use, but the pattern is usually the same:
1) Pick an email provider that supports event webhooks
Common providers:
- SendGrid
- Mailgun
- Postmark
- Amazon SES
- Mailchimp Transactional / Mandrill
- Resend (event webhooks for delivery/open/click, depending on plan/features)
These providers can send HTTP requests to your server whenever an event happens:
- delivered
- bounced
- opened
- clicked
- unsubscribed
- complained/spam report
2) Create a webhook endpoint in your app
Make an HTTPS endpoint that accepts POST requests, for example:
POST https://yourapp.com/webhooks/email-events
Your endpoint should:
- return
200 OKquickly - parse the event payload
- verify the request signature if the provider supports it
- store the event in your database
- update your app state accordingly
Example logic:
- if event =
bounce, mark recipient as bounced - if event =
open, increment open count / mark as opened - if event =
click, track clicked links
3) Register that endpoint in your email provider dashboard
In the provider settings, configure:
- webhook URL
- event types to send
- signing secret / verification key
Examples:
- SendGrid: “Event Webhook”
- Mailgun: “Webhooks”
- Postmark: “Webhooks”
- SES: SNS notifications for bounces/complaints/deliveries
4) Verify webhook authenticity
Always verify the sender so random clients can’t fake events.
Common approaches:
- HMAC signature in headers
- public key signature
- shared secret token
- provider-specific verification flow
5) Handle open tracking carefully
“Open” tracking usually works by embedding a tracking pixel in the email body. Important caveats:
- Some email clients block images, so opens may not register.
- Apple Mail Privacy Protection can cause false opens.
- Some security scanners open emails automatically.
- Opens are useful as a rough signal, not perfect truth.
So for important workflows, rely more on:
- delivery
- bounce
- click
- reply
- conversion events in your app
6) Test with provider sandbox/tools
Most providers offer:
- test event payloads
- webhook simulators
- sandbox/test mode
Use those to make sure your endpoint handles:
- bounce
- deferred
- delivered
- open
- click
- complaint
Example webhook endpoint
Here’s a simple Node/Express example for receiving events:
import express from "express";
const app = express();
app.use(express.json());
app.post("/webhooks/email-events", async (req, res) => {
const events = req.body;
for (const event of events) {
switch (event.event) {
case "bounce":
console.log("Bounce:", event.email);
// update DB: mark bounced
break;
case "open":
console.log("Opened:", event.email);
// update DB: mark opened
break;
case "delivered":
console.log("Delivered:", event.email);
break;
case "click":
console.log("Clicked:", event.email, event.url);
break;
}
}
res.sendStatus(200);
});
app.listen(3000);
If you’re using AWS SES
SES doesn’t directly “push” all events the same way some providers do; it usually uses:
- SNS topics
- optionally EventBridge
- then you subscribe your app/server to those events
Typical flow:
- Configure SES event publishing
- Send bounce/open/delivery events to SNS or EventBridge
- Subscribe your endpoint or Lambda
- Process the notifications
If you tell me which email provider you’re using and what backend stack you have (Node, Python, Rails, etc.), I can give you the exact webhook setup and sample code.