Prompt
I'm building an admin dashboard that sends alerts and invoices. How should I structure the email sending side?
Latest observation
A good way to structure the email-sending side is to treat it as a small, reliable subsystem rather than calling your email provider directly from UI actions.
Recommended structure
1) Separate email intent from email delivery
Your admin dashboard should create events like:
AlertCreatedInvoiceGeneratedPaymentDueReminderRequested
Then a background worker turns those into actual emails.
This gives you:
- retries on failure
- less risk of slowing down the dashboard request
- easier auditing
- easier testing
- the ability to switch providers later
2) Use an email service layer
Have one central module/service responsible for sending emails.
Example responsibilities:
- choose the right template
- merge data into the template
- validate recipient addresses
- call the email provider API
- log result/failed attempts
- record delivery status in your DB
A simple structure:
email/templates/email/services/EmailServiceemail/jobs/SendEmailJobemail/providers/SendGridProviderorSESProvideremail/logs/email_outboxtable
3) Implement the outbox pattern
For reliable delivery, store outgoing emails in a database table first.
Example email_outbox fields:
idtype(alert,invoice,reminder)to_emailtemplate_keypayload(JSON)status(pending,sent,failed)attempt_countlast_errorcreated_atsent_at
Flow:
- Dashboard action creates domain record.
- App inserts email request into
email_outbox. - Worker picks pending rows.
- Worker sends email.
- Worker marks as sent or failed.
This is especially useful for invoices, where you want strong guarantees and auditability.
4) Make templates first-class
Use separate templates for:
- alert notifications
- invoice emails
- payment reminders
- receipt emails
Keep them parameterized, e.g.:
- customer name
- invoice number
- due date
- total amount
- dashboard link
If you need both HTML and text, generate both.
5) Keep sending asynchronous
Do not send emails directly in the request/response cycle unless it’s a low-volume prototype.
Use:
- background jobs
- message queue
- scheduled workers
Common choices:
- Sidekiq + Redis
- Celery + RabbitMQ/Redis
- BullMQ
- SQS + worker
- Hangfire
- a cron-driven worker if simple
6) Add retry and failure handling
Email providers fail sometimes.
Recommended behavior:
- retry transient errors
- exponential backoff
- cap retry count
- mark permanent failures clearly
- alert admins if invoice emails fail repeatedly
For example:
- retry 3–5 times
- only retry 5xx and network errors
- don’t retry invalid email addresses
7) Track delivery and audit logs
For an admin dashboard, auditability matters.
Store:
- who triggered the email
- which customer it was sent to
- which invoice/alert it relates to
- provider message ID
- timestamps
- status changes
This helps with support and compliance.
8) Use one provider abstraction
Don’t let the rest of your app depend on SendGrid/Mailgun/SES directly.
Create an interface like:
send(message): result
Then implement:
SendGridProviderAmazonSESProvider
This makes provider switching easier.
Suggested architecture
Admin UI
↓
API / Controller
↓
Domain service (create alert/invoice)
↓
Email outbox row created
↓
Queue / Worker
↓
Email service
↓
Provider API
↓
Status + logs updated
Practical tips for alerts vs invoices
Alerts
- usually immediate
- can tolerate a bit of delay
- often lower importance than invoices
- consider digesting if high volume
Invoices
- should be highly reliable and auditable
- tie the email to a persistent invoice record
- consider versioning the template used
- preserve the exact sent content for records
Minimal implementation pattern
If you want something simple but solid:
- Create an
emailstable / outbox table. - Write a background worker that polls pending emails.
- Use a template engine for rendering.
- Call a provider API through a single
EmailSenderabstraction. - Store logs and retry failures.
One good rule of thumb
If the email matters financially or operationally, never “fire and forget” it from the web request. Put it through a persisted queue/outbox.
If you want, I can sketch:
- a database schema for the outbox
- example code in your stack
- a full flow for alerts + invoices