You restore a production backup onto staging to debug an order that went wrong. The restore brings the database with it — orders, customers, and the SMTP credentials that production uses to send mail. You reprocess the order to reproduce the bug, and WooCommerce does exactly what it is designed to do: it emails the customer a second copy of an order they placed last week.
Nothing malfunctioned. Every part of that chain worked correctly. The problem is that copying a site copies its ability to send mail, and nothing in WordPress treats “this is a staging copy” as a fact worth acting on.
Why this keeps happening
WordPress has no concept of an environment boundary where mail is concerned. wp_mail() behaves identically on production, staging and a laptop. If credentials are present and a plugin calls wp_mail(), mail goes out.
WP_ENVIRONMENT_TYPE exists and can report staging, but core does not consult it before sending. It is a signal available to plugins that choose to read it, not a safety mechanism.
That leaves the responsibility with whoever set up staging — and the failure mode is silent. A staging site that sends nothing looks identical to a staging site that is quietly emailing customers, right up until somebody replies.
The usual workaround, and where it breaks
The standard advice is a stack of plugins: one to disable emails, another to log what would have been sent, and a reminder to deactivate your SMTP plugin on staging.
It works, but it depends on a person remembering to do three things every time a staging site is created or refreshed. Each refresh restores the production database over the top, which is exactly when settings revert and the arrangement quietly comes undone.
The deeper issue is placement. Those plugins sit alongside the mail system rather than inside it. Anything that talks to a provider’s API directly, rather than going through wp_mail(), walks straight past them.
Closing it at the boundary instead
The reliable place to stop staging mail is the point where the site hands a message to a transport, because everything funnels through it regardless of which plugin generated the message.
NimblyMail’s Developer Sandbox works there, with two modes:
- Log mode short-circuits sending entirely. The message is recorded in the email log with its full headers and body, and no connection to your provider is opened at all — nothing is transmitted, so nothing can leak.
- Redirect mode rewrites every recipient to one safe address. The original To, Cc and Bcc are preserved in
X-NimblyMail-Original-*headers, and the subject gets a configurable prefix —[SANDBOX]by default — so a redirected message is impossible to mistake for a real one.
Two design details matter more than the modes themselves.
It fails closed. If the sandbox is enabled and the mode value is anything the code does not recognise, it falls back to log mode rather than delivering. A typo in a configuration constant results in mail being withheld, not sent.
The allowlist is strict by design. You can allow specific addresses through — your own, or your whole team’s domain — but the rule is evaluated against every recipient on the message. If a message goes to you and a customer, the customer’s address fails the check and the entire message is sandboxed. One stray recipient sandboxes the whole send rather than letting it through to everybody.
Allowlist entries are either an exact address or a domain wildcard such as *@yourteam.com, one per line, with # for comments. It is a literal match, not a regular expression.
Setting it up
- On the staging site, go to NimblyMail → Sandbox and enable it.
- Pick Log if you want nothing to leave the server, or Redirect with a single test address if you want to see the rendered mail in an inbox.
- Optionally add your own address or
*@yourteam.comto the allowlist. - Trigger a real email and confirm it appears in the log marked
sandboxed.
Because settings can be pinned with PHP constants, the durable version of this is to put the sandbox configuration in the staging site’s wp-config.php. A constant outranks the database, so restoring a production database over staging cannot turn the sandbox off — which is the exact moment the plugin-stack approach fails.
The wider problem: what else leaks when you clone
Email is the loudest failure, because customers notice. It is not the only thing a clone inherits, and the same fix applies to several of them.
- Payment gateways in live mode. A cloned store can take real payments. Most gateways have a test mode; confirm it after every refresh.
- Scheduled tasks. Subscription renewals, abandoned-cart reminders and back-in-stock notifications all run on cron, and a clone runs them against real customer data. The reported real-world incident behind this article was exactly that — a restore triggering back-in-stock emails to live customers.
- Third-party integrations. CRM syncs, shipping labels, accounting exports. These call APIs directly and never touch
wp_mail(), so an email-blocking plugin does not stop them. - Search engine indexing. Not urgent, but a staging site in the index competes with production.
The pattern is that anything holding production credentials will use them. The durable fix is the same for all of it: keep environment-specific configuration in wp-config.php rather than the database, so restoring a production database cannot carry production behaviour with it.
Making it survive the next refresh
The reason the plugin-stack approach fails is not that the plugins are bad. It is that the guard lives in the database, and refreshing staging overwrites the database.
So put the guard where the restore cannot reach it. Settings defined as PHP constants outrank stored options, which means a constant in the staging site’s wp-config.php survives every refresh:
define( 'NIMBLYMAIL_SANDBOX_ENABLED', true );
define( 'NIMBLYMAIL_SANDBOX_MODE', 'log' );
Now restoring production over staging cannot switch sending back on, because the constant wins regardless of what the database says. That single property is the difference between a control that works and one that works until somebody refreshes.
Two related habits worth adopting:
- Set
WP_ENVIRONMENT_TYPEtostagingin the same file. Core does not act on it for mail, but plugins increasingly read it and it costs nothing. - Make the state visible. The most consistent complaint about staging guards is not that they are missing but that nobody can tell which position the switch is in. An always-present admin-bar indicator answers that at a glance, which matters when several people share an environment.
Verifying it actually works
Do not take the settings screen’s word for it. Trigger a genuine transactional email — place a test order, or request a password reset — and check the email log. A sandboxed message is recorded with a status of sandboxed and a note of which mode handled it.
Seeing the message in the log with that status is the proof: it was generated, it was intercepted, and it was not delivered.
