Prompt
How do I keep track of failed email sends?
Latest observation
To keep track of failed email sends, use a combination of logging, retries, and alerting. The best approach depends on whether you’re sending emails from an app, a marketing tool, or a server.
1. Log every send attempt
Store details for each email attempt:
- Recipient
- Subject/message ID
- Timestamp
- Status:
queued,sent,failed,bounced - Error message / provider response
- Retry count
This gives you an audit trail and makes failures easy to inspect later.
2. Capture provider webhooks
If you use a service like SendGrid, SES, Mailgun, or Postmark, enable their event webhooks for:
- Delivery
- Bounce
- Complaint
- Deferred
- Drop/failure
These are more reliable than only checking your app’s immediate send response.
3. Retry transient failures
Some failures are temporary:
- Timeout
- Rate limit
- Mail server unavailable
Use a retry policy with:
- Exponential backoff
- Max retry limit
- Dead-letter queue or failure bucket after retries are exhausted
4. Separate permanent vs temporary failures
Classify errors:
- Permanent: invalid address, blocked recipient, spam rejection
- Temporary: network error, mailbox full, graylisting
Permanent failures should usually stop retrying and be marked clearly.
5. Add alerts and dashboards
Set up monitoring for:
- Failure rate spikes
- Bounce rate spikes
- Queue backlog
- Repeated provider errors
Send alerts via Slack, email, or PagerDuty when thresholds are exceeded.
6. Keep a failure store
Common options:
- Database table for failed sends
- Log aggregation system (ELK, Datadog, CloudWatch)
- Queue/DLQ for messages that couldn’t be delivered
A simple database table might include:
idrecipientsubjectstatuserror_codeerror_detailsattemptslast_attempt_atcreated_at
7. Make failures visible to users or admins
If relevant, show a “failed” state in the UI so support or users can see which emails didn’t send and why.
If you want, I can also show you:
- a database schema for tracking failed emails,
- sample code for a specific language,
- or a recommended retry workflow.