Prompt
I'm building a developer tool and need a transactional email setup that works cleanly across dev, staging, and prod. What does that usually look like?
Latest observation
A clean transactional email setup across dev, staging, and prod usually has one goal:
- real user emails only go out from prod
- non-prod environments can test email flows safely
- templates, configuration, and observability are shared as much as possible
Here’s what that typically looks like.
1) Separate environments, separate email behavior
Production
- Uses the real email provider account and real sending domain
- Sends to actual customer addresses
- Has SPF/DKIM/DMARC configured
- Has bounce/complaint handling enabled
- Uses dedicated sending domains/subdomains like:
mail.yourapp.comnotify.yourapp.com
Staging
Usually one of these approaches:
-
Send to a sandbox/inbox capture service
- Example: Mailtrap, MailHog, Ethereal, etc.
- Emails are captured and viewable in a UI
- No real delivery happens
-
Send real emails only to a whitelist
- Only allowed internal addresses can receive mail
- All other recipients are rewritten to a test inbox
- Useful if you want to test the provider and templates end-to-end
-
Disable sending entirely
- Email jobs are created, but the provider call is mocked or short-circuited
- Good for fast integration tests, but less realistic
Development
- Usually the lightest setup:
- local SMTP catcher like MailHog/Mailpit, or
- provider sandbox API, or
- fully mocked transport
- Developers can inspect content locally without sending anything externally
2) A shared email abstraction in code
Most teams put all sending behind a small interface, e.g.:
sendWelcomeEmail(user)sendPasswordReset(user, token)sendReceipt(order)
Then the implementation depends on environment:
dev: local catcher / mockstaging: sandbox or whitelisted deliveryprod: real provider
This keeps app code from knowing about environment-specific mail logic.
3) Environment-driven configuration
Common config split:
EMAIL_PROVIDER=smtp|ses|sendgrid|postmark|mailgunEMAIL_FROM=no-reply@yourapp.comEMAIL_REPLY_TO=support@yourapp.comEMAIL_ENV=dev|staging|prodEMAIL_DELIVERY_MODE=mock|capture|realEMAIL_ALLOWLIST=alice@company.com,bob@company.com
Typical behavior:
- dev:
mockorcapture - staging:
captureorallowlist - prod:
real
4) Use templates, but render them the same way everywhere
A good setup usually has:
- one template system
- one rendering pipeline
- one source of truth for subject/body variants
Examples:
- React/email components
- MJML
- Handlebars / Liquid / Nunjucks
- provider-native templates if you’re okay with vendor lock-in
Best practice:
- render emails in app code or a shared template repo
- preview them in staging
- test them in CI
- avoid having logic split between app and provider dashboard if you can
5) Prevent accidental sends from non-prod
This is the most important safety layer.
Common guardrails:
- block sending unless
ENV=prod, or unless recipient is allowlisted - rewrite all outbound recipients in staging to a single internal inbox
- prepend subject lines in non-prod:
[STAGING] Password reset[DEV] Welcome
- add a visible banner in email body for non-prod emails
- disable all customer-facing triggers in staging unless explicitly enabled
Many teams use both:
- environment check
- recipient allowlist
That way, even if config is wrong, you’re still protected.
6) Use a catch-all test inbox or inbox service in staging
For staging, a common pattern is:
staging-mail@yourcompany.com- all outgoing test emails route here
- developers inspect the inbox to validate formatting, links, localization, and deliverability behavior
This is especially useful for:
- password reset
- invite emails
- onboarding flows
- receipt flows
- unsubscribe/preference links
7) Handle async delivery with a job queue
Transactional email is often sent asynchronously:
- user action creates an email job
- queue worker sends it
- retries happen on transient failures
This gives you:
- better UX
- retry support
- easier instrumentation
- less coupling to request latency
In staging/dev, the worker may still run, but its transport is mocked/captured.
8) Test what matters
Good test coverage usually includes:
Unit tests
- template rendering
- recipient selection
- subject generation
- env-based routing logic
Integration tests
- job creation
- provider API wrapper
- sandbox/capture behavior
End-to-end checks
- link formatting
- unsubscribe flows
- localization
- attachment handling if relevant
For email specifically, snapshot tests on rendered HTML can be very effective.
9) Observability and audit trail
A solid transactional email system logs:
- message ID
- environment
- template name
- recipient
- status
- provider response
- retry count
- bounce/complaint events
In prod, also track:
- send rate
- bounce rate
- complaint rate
- delivery failures
- latency
This helps distinguish:
- code bug
- provider issue
- DNS/auth issue
- content filtering issue
10) Recommended setup by environment
Dev
- MailHog / Mailpit / local SMTP catcher
- no real sends
- easy preview UI
- fast iteration
Staging
- same code path as prod
- sandbox provider OR recipient allowlist
- clearly marked messages
- QA can inspect actual rendered email
Prod
- real provider
- dedicated sending domain
- proper DNS auth
- bounce/complaint webhooks
- retries and monitoring
A practical “default” architecture
If you want a simple, robust default:
- One email service module
- One template system
- One provider adapter
- Three delivery modes:
mockfor local devcaptureorallowlistfor stagingrealfor prod
- Queue-based sending
- Strict non-prod guardrails
- Full logging and webhook handling
Example policy
A very common policy is:
dev: never send externallystaging: only send to@yourcompany.comprod: send normally
Plus:
- prefix non-prod subjects
- rewrite recipients in staging
- require env-specific API keys
- separate sender domains if possible
If you want, I can also sketch:
- a reference architecture diagram,
- a config matrix for dev/staging/prod, or
- an example implementation in Node, Python, or Go.