Prompt
building a multi-environment email setup for dev staging prod
Latest observation
Oct 13, 2026
OpenAI APIWeb search: off
Here’s a practical way to set up multi-environment email for dev / staging / prod without accidentally spamming real users.
Recommended pattern
1) Use separate email configs per environment
Each environment should have its own:
- SMTP/API credentials
- From address
- Reply-to
- Mail provider settings
- Recipient restrictions
Example:
- dev: use a sandbox provider or local mail catcher
- staging: use a test inbox/domain or provider sandbox
- prod: real provider and real delivery
Suggested environment behavior
Dev
Goal: safe testing, no real emails.
Options:
- Mailtrap
- MailHog
- Papercut
- Local SMTP catcher
Typical setup:
- intercept outgoing mail
- display in a UI or logs
- no external delivery
Staging
Goal: test actual email flow, but still safe.
Options:
- email provider sandbox mode
- send only to internal test accounts
- rewrite all recipients to a test inbox
- use a staging-only domain like
staging.example.com
Typical setup:
- real templates
- real rendering
- deliverability checks
- no customer-facing sends
Prod
Goal: reliable delivery to real users.
Use:
- verified sending domain
- SPF, DKIM, DMARC
- production API keys
- monitoring/bounce handling
Core best practices
1) Centralize email sending
Have one abstraction like:
EmailServiceMailerNotificationSender
Then switch behavior by environment.
2) Guard recipients in non-prod
A strong safety mechanism:
- dev/staging should never send to arbitrary external addresses
- either:
- block sending outside an allowlist
- rewrite all recipients to one internal inbox
- use provider sandbox
3) Use environment-specific templates
At minimum, environment-specific:
- branding
- footer text
- links
- asset URLs
Example:
- dev links point to dev app
- staging links point to staging app
- prod links point to production app
4) Separate domains/subdomains
Recommended:
dev-mail.example.comstaging-mail.example.commail.example.com
Or at least:
no-reply@dev.example.comno-reply@staging.example.comno-reply@example.com
5) Manage secrets properly
Store credentials in:
- environment variables
- secret manager
- CI/CD protected variables
Do not hardcode:
- SMTP passwords
- API keys
- provider tokens
Example config structure
# common
APP_ENV=staging
# dev
DEV_MAIL_PROVIDER=mailhog
DEV_MAIL_HOST=localhost
DEV_MAIL_PORT=1025
# staging
STAGING_MAIL_PROVIDER=sendgrid
STAGING_MAIL_API_KEY=...
STAGING_MAIL_FROM=no-reply@staging.example.com
STAGING_MAIL_ALLOWLIST=test1@company.com,test2@company.com
# prod
PROD_MAIL_PROVIDER=sendgrid
PROD_MAIL_API_KEY=...
PROD_MAIL_FROM=no-reply@example.com
Safety rules I’d recommend
For dev and staging:
- block unknown recipients
- strip production webhooks/callbacks
- disable unsubscribe processing if needed
- tag emails with environment headers:
X-Environment: staging
For prod:
- enable bounces and complaints handling
- monitor send rates
- alert on failures
- verify domain authentication
Testing checklist
Dev
- email renders
- links are correct
- attachments work
- no real delivery
Staging
- sandbox or allowlist works
- templates match prod
- provider integration works
- tracking/bounces not broken
Prod
- deliverability verified
- DNS records correct
- reply handling works
- monitoring in place
A simple implementation approach
Pseudo-logic:
if (env === 'dev') {
useLocalSMTP();
} else if (env === 'staging') {
useSandboxOrAllowlist();
} else {
useProductionProvider();
}
And always include a recipient guard:
if (env !== 'prod' && !allowedRecipients.includes(to)) {
throw new Error('Blocked non-prod email recipient');
}
My recommendation
If you want the cleanest setup:
- Dev: MailHog or Mailtrap
- Staging: provider sandbox or recipient allowlist
- Prod: real provider with authenticated domain
- One shared mailer abstraction
- Recipient guard rails everywhere
If you want, I can also give you:
- a Node.js / Python / Rails / Django example,
- a Terraform/DNS checklist for SPF/DKIM/DMARC,
- or a full architecture diagram for this setup.