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.

A copy of production data goes to staging and passes three layers: clean data, a mail setting that cannot deliver, and an allowlist of test domains. Letters to real customers are blocked; letters reach a mail catcher or a test domain.
Three layers between staging and your customers. Any one of them stops the letter, and together they still hold when one of them fails.

Where these letters come from

When this happens, the cause is almost always on this list:

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:

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:

FrameworkSetting that sends nothing
LaravelMAIL_MAILER=log
Djangoconsole, file or in-memory email backend
Railsdelivery_method set to :test or :file
SymfonyMAILER_DSN=null://null
Node.jsNodemailer 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:

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) return

This 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:

  1. 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.
  2. Nothing should arrive in that mailbox. The letter should show up in your catcher or in the blocked-letter log instead.
  3. 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.)
  4. 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

  1. Stop sending. Stop the staging workers and scheduler, and disable or delete the staging mail key. Do this before investigating.
  2. Find out who got what. The provider's delivery log lists recipients, times and subjects. Export it before it rotates.
  3. 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.
  4. 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.
  5. 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

← Back to all guides