Stop your staging site from emailing real customers
Someone copies the production database to staging so the team can test on realistic data. The copy includes every customer's email address, and staging runs the same scheduled jobs as production. Overnight it sends 14,000 "your trial ends tomorrow" reminders, each with a link to the staging site. By morning support is answering confused customers, and some of them have already clicked.
Nobody decided to email customers from staging. A few ordinary settings lined up. This guide shows where such letters come from and how to stop them in three layers, so that one forgotten setting is not enough to cause it again.
Where these letters come from
When this happens, the cause is almost always on this list:
- Scheduled jobs on a fresh copy. Reminders, digests and renewal notices run on a timer. On an older copy many "due" dates are already in the past, and the record of what production has already sent may be missing, so the first run sends everything at once.
- A queue copied with the database. If unsent jobs live in a database table, the copy brings them along. Staging workers start and send letters production never got to.
- The production mail key in staging. Configuration was copied from production, or a shared settings file is used by both. Staging then sends through your real account, from your real sender address.
- A default that falls back to real delivery. The code reads a mail setting from the environment and falls back to real delivery when the variable is missing. A new server without that variable sends for real.
- A tester presses a button. "Notify all users about the new feature" works exactly as designed, on real addresses.
- Someone else sends for you. Payment, booking and support services can email your customers directly. If staging talks to them with live credentials, the receipt goes out from their side, and your mail setting never sees it.
Layer 1: clean the data before it reaches staging
The most reliable fix is to make sure staging has no real addresses to write to. Rewrite every email address as part of the copy, before anyone starts the application. A common pattern keeps the user's id, so you can still find the right account in tests (MySQL syntax; PostgreSQL joins strings with ||):
UPDATE users SET email = CONCAT('user-', id, '@example.test');Addresses on the reserved names .test, .example and .invalid, and on example.com, are set aside for testing and documentation. Mail to them should not be delivered to anyone, so a letter that slips through goes nowhere instead of to a stranger.
The users table is rarely the only place with addresses. Search the whole schema for them:
- billing and invoice contacts, team invitations, newsletter lists;
- copy fields on orders and tickets ("also notify"), reply-to addresses;
- addresses inside JSON or text columns, such as saved form submissions;
- phone numbers, if your project also sends text messages: the same accident happens with SMS.
If the letters will be read in a public mailbox (see below), go further: replace names, postal addresses and order details with made-up values too. An address rewritten to a test domain still carries the customer's name in the greeting.
Make the rewrite part of the copy script itself, not a step someone remembers. A copy made in a hurry on a Friday evening is exactly the one that skips manual steps.
Layer 2: make staging unable to deliver
Even with clean data, staging should not be able to hand a letter to the outside world unless you mean it to. Most frameworks have a mode that sends nothing. Some write letters to a log or a file, others keep them in memory or simply discard them:
| Framework | Setting that sends nothing |
|---|---|
| Laravel | MAIL_MAILER=log |
| Django | console, file or in-memory email backend |
| Rails | delivery_method set to :test or :file |
| Symfony | MAILER_DSN=null://null |
| Node.js | Nodemailer with the JSON or stream transport |
If testers need the letters in one place instead, most frameworks can also send every letter to a single address: Mail::alwaysTo() in Laravel, a mail interceptor in Rails, the envelope recipients setting in Symfony, or a small custom backend in Django.
Letters written to a log are hard to check by eye. A self-hosted mail catcher such as Mailpit accepts every letter, delivers none and shows them in a web page, with links and attachments you can open. That is usually the best choice for staging that holds data copied from production.
Two settings matter as much as the choice of tool:
- Fail closed. If the environment name is missing or unknown, treat it as staging. Only an explicit "production" may send for real.
- A separate account at the mail provider. Give staging its own API key, or none at all. The production key should not exist on any other server, so a copied configuration file cannot send from your real account.
Layer 3: allow only test domains at the last step
The last layer sits in the code that actually sends. Outside production, it checks each recipient against a short list of allowed domains and drops, or redirects, anything else:
allowed = ["example.test", "qa.your-domain.com"] if environment != "production" and recipient_domain not in allowed: log("blocked letter to " + recipient) returnThis catches what the first two layers miss: an address typed by a tester, an import that ran after the copy, a job added last week that bypasses the usual mail settings. Log every blocked letter. A sudden pile of blocked letters is the early warning that something on staging is trying to email people.
Reading staging letters on your own test domain
A catcher only works for people who can reach it. When testers, a client or a phone app need to open the letters, point a test subdomain such as qa.your-domain.com at this site, as described in the setup guide. Then rewrite staging addresses to that subdomain, for example user-1042@qa.your-domain.com, and anyone who knows the address can open that mailbox here and see exactly what the account received.
Only do this with data you have already cleaned. Every mailbox on this site is public: anyone who types the same address sees the same letters. Names, orders and invoices copied from production must not end up in a public inbox. Clean the data first (layer 1), and keep a self-hosted catcher for anything that still holds real customer details.
Check it before you trust it
A guard you never tested is a guess. After each copy from production, and after any change to mail settings, try to break it on purpose:
- Create a staging user with an address on a real domain you control, for example a colleague's work mailbox, and ask for a password reset.
- Nothing should arrive in that mailbox. The letter should show up in your catcher or in the blocked-letter log instead.
- Request the same reset for an address on your test subdomain. That letter should appear in its mailbox here. (This step only applies if staging is allowed to deliver for real, limited by layer 3.)
- Check the staging mail account at your provider: it should show no deliveries outside your test domains.
Add these steps to the checklist of the copy script, next to the rewrite. When test letters reach the right place, the next job is checking what is inside them; that is covered in Test your project's emails before launch. If they do not arrive at all, see Your app's test emails are not arriving.
If it has already happened
- Stop sending. Stop the staging workers and scheduler, and disable or delete the staging mail key. Do this before investigating.
- Find out who got what. The provider's delivery log lists recipients, times and subjects. Export it before it rotates.
- Close staging to the public. Customers now hold links to it. Put it behind a password or turn it off, so nobody can log in to a copy of their own account there.
- Tell people briefly and honestly. One short letter from your production system: the earlier message was sent by mistake from a test system, please ignore it, and, if it is true, that nothing on their account changed.
- Check your obligations. If the letters included other people's personal data, the incident may count as a data breach where you operate, with a deadline to report it. Ask whoever handles privacy for your project.
Short version
- Rewrite every address during the copy, onto reserved test names or your own test subdomain.
- Staging sends nothing by default and delivers for real only when told to, with its own mail account.
- The last step lets through test domains only, and logs what it blocks.
- Test the guard after every copy: a reset to a real address must not arrive.
- Public test mailboxes are for cleaned data only.