Keep staging emails away from real recipients: point SMTP at MailBrew, or rewrite only the recipient domain
The problem
When you copy production data into staging, real customer email addresses come with it. Run the signup flow or a newsletter job against that data and real people get your test emails.
Turning email off entirely is not great either: QA can no longer open the actual message and click its links. What you want is for the sending code to run exactly as it does in production, while the messages are caught before they leave and kept somewhere you can read them.
Common approaches and what each one costs you
| Approach | What you lose |
|---|---|
Send everything to one address (for example Laravel's Mail::alwaysTo()) |
The original recipients. Afterwards you cannot tell who each message was meant for |
Stop sending (MAIL_MAILER=log and similar) |
You cannot open the message to check how it looks or click its links |
| Run a local catcher such as Mailpit on your machine or a server | Fine for local development. For staging, you have to host it and build your own way to control who can see it |
On the first point: Laravel's Mail::alwaysTo() replaces the To recipients with the address you give it, then removes Cc and Bcc. A message sent to several people via Bcc turns into a single message to that one address.
Option 1: point your SMTP host at MailBrew
Change your app's SMTP host (MAIL_HOST in Laravel) to MailBrew. When a message is sent with SMTP authentication, MailBrew accepts it whoever the recipient is and does not deliver it to the original recipient.
- An owner opens "SMTP Settings" in the dashboard and clicks "Generate SMTP credentials". The password is shown only at that moment.
- Use "Copy .env config" to copy settings you can paste into your staging
.env. Send over an encrypted connection (port 587 with STARTTLS, or port 465). Authentication over an unencrypted connection is refused. - Send mail from the app as usual. Messages show up in the "Mailbox".
The original recipients are kept. A message sent to [email protected] can be fetched through the API by that address. The lookup uses the actual SMTP recipient (RCPT TO), not the To header, so Bcc recipients work too.
# Wait for mail sent to [email protected] (timeout in seconds, up to 60)
curl "https://mailbrew.dev/api/mailbrew/emails/[email protected]&timeout=30" \
-H "X-API-Key: $MAILBREW_API_KEY"
?to= only returns messages sent with the SMTP credentials of the API key's company. If you register a company webhook, you also get a notification for each external recipient.
Two things to keep in mind:
- Each company has one set of SMTP credentials. "Regenerate" stops every environment that uses the current credentials from sending.
- The mail does not go through your production sending service, so you cannot check SPF, DKIM, or spam filtering on that route. If you need those, use option 2.
Option 2: rewrite only the recipient domain
Leave your app's sending setup alone (the same route as production, such as Amazon SES) and rewrite only the recipient's domain to a MailBrew receiving domain.
Receiving domains and automatic address creation
Each company has a receiving domain such as acme.mailbrew.dev. It is shown as "Receiving domains" at the top of the "Addresses" page in the dashboard. If you have registered and verified a custom domain, you can use that domain too.
Mail sent to this domain arrives even if you have not created the address first. When a message arrives for a new recipient, an address is created for it automatically, so you can tell messages apart by recipient.
| Question | For addresses created on receipt |
|---|---|
| Setup | Your company needs at least one active API key. Without one, mail to addresses you have not created is refused |
| Lifetime | 1 hour, extended to 1 hour from each new message |
| After it expires | You can still read the messages through the API and in the mailbox for your company's retention period |
| Address limit | Not counted. Received messages do count toward your monthly email count |
| Which API keys can read it | Any API key of the same company |
How to rewrite the address
If you only swap the domain, [email protected] and [email protected] both end up at taro@…. Put the whole original address into the local part (the part before @) instead. Nothing gets mixed up, and you can still read who the message was for.
[email protected] → [email protected]
[email protected] → [email protected]
Refuse to send when the rewrite did not happen
The failure you most want to avoid with this approach is sending through your production route with the rewrite missing. So build it to fail closed: if the address could not be rewritten, the message is not sent.
Laravel fires a MessageSending event right before sending, and if a listener returns false, the message is not sent. The listener below rewrites recipients outside production, and stops the message when no target domain is configured or when any other domain is left after the rewrite.
<?php
namespace App\Listeners;
use Illuminate\Mail\Events\MessageSending;
use Symfony\Component\Mime\Address;
class RedirectNonProductionMail
{
public function handle(MessageSending $event): ?bool
{
if (app()->environment('production')) {
return null; // leave production alone
}
// e.g. acme.mailbrew.dev (add 'redirect_to_domain' => env('MAIL_REDIRECT_TO_DOMAIN') to config/mail.php)
$domain = config('mail.redirect_to_domain');
if (! $domain) {
return false; // no target domain: do not send
}
$message = $event->message;
$suffix = '@'.strtolower($domain);
$rewrite = fn (array $list) => array_map(
// 書き換え済みの宛先には触らない(リスナーが 2 回登録されても宛先が壊れない)
fn (Address $a) => str_ends_with(strtolower($a->getAddress()), $suffix)
? $a
: new Address(str_replace('@', '_at_', $a->getAddress()).$suffix, $a->getName()),
$list,
);
if ($to = $message->getTo()) {
$message->to(...$rewrite($to));
}
if ($cc = $message->getCc()) {
$message->cc(...$rewrite($cc));
}
if ($bcc = $message->getBcc()) {
$message->bcc(...$rewrite($bcc));
}
// Belt and braces: if any other domain is left, do not send
foreach ([...$message->getTo(), ...$message->getCc(), ...$message->getBcc()] as $a) {
if (! str_ends_with(strtolower($a->getAddress()), $suffix)) {
return false;
}
}
return null;
}
}
On Laravel 11 and later, placing it in app/Listeners is enough: it is registered automatically. If you have turned event discovery off, register it in boot() of your AppServiceProvider with Event::listen(MessageSending::class, RedirectNonProductionMail::class);. Even if it ends up registered twice, addresses that are already rewritten are left alone, so recipients do not get mangled.
The key is the shape of the environment check: production passes through untouched, and everywhere else a message is sent only if it was rewritten. If you write it the other way round ("rewrite when the environment is staging"), a typo in the environment name means mail goes out unrewritten. A blocked message does not throw; it is simply not sent. A test that waits for the email will catch it, but logging why the message was blocked makes it much easier to track down.
Option 1 can fail too, if a misread setting sends mail back through your production service. Adding a check to the same listener that refuses to send when the SMTP host is not MailBrew outside production closes that gap.
Which one to use
| What you want to check | Option 1: SMTP host | Option 2: recipient domain |
|---|---|---|
| Subject, body, and links | Good fit | Good fit |
| Nothing leaks if recipient rewriting is missed | Good fit (a wrong SMTP host is covered by the check below) | Needs the safeguard above |
| SPF, DKIM, and spam filtering on your production route | Not possible | Good fit |
| Knowing the original recipient | Fetch with ?to= in the API |
Kept in the local part |
| Changes to your app | Settings such as MAIL_HOST only |
Code that rewrites recipients |
You can use both. For example, catch everything with option 1 day to day, and send from an option 2 environment on the days you want to check your sending setup.
For automated tests, create disposable addresses with the API
When each test run needs a fresh recipient, as in E2E tests, create an address through the API. ttl is in seconds; after that the address expires. With the Node.js SDK (@mailbrew/sdk):
const { MailBrew } = require('@mailbrew/sdk');
const mb = new MailBrew(process.env.MAILBREW_API_KEY);
// Create an address that lives for 10 minutes
const addr = await mb.createAddress({ ttl: 600 });
// ...make the app under test send an email to addr.address...
// Wait for it to arrive (timeout in milliseconds, up to 60 seconds)
const email = await mb.waitForEmail(addr.address, { timeout: 30000 });
console.log(email.subject);
// Clean up
await mb.deleteAddress(addr.address);
Waiting uses server-side long polling and returns as soon as the message arrives. Addresses created through the API count toward your plan's address limit. If you would rather not think about that, send to any name at your receiving domain, as in option 2, and wait on that address instead (tag-based addresses).
The API key needs addresses:write to create addresses, emails:read to fetch and wait for mail, and addresses:delete to delete them.
Letting people outside the dev team see staging email
Sometimes QA staff or your client need to look at staging email. For that, an owner adds them under "Members" with the "Mailbox only" role.
A "Mailbox only" member can open messages in the mailbox, download raw messages and attachments, and manage their own account settings, nothing else. They cannot delete messages or open the API keys, SMTP, or webhook pages.
The mailbox lists every message your company received. If you need to separate what each client can see, create a separate account (company) per client. Opening a message is recorded in the audit log. For two-factor enforcement and IP restrictions, see Access control for test inboxes.
Summary
| Goal | How |
|---|---|
| Nothing leaves, whoever the recipient is | Point your SMTP host at MailBrew (SMTP Settings) |
| Check SPF and DKIM on your production sending route | Rewrite the recipient domain to your receiving domain |
| A missed rewrite does not leak mail | A listener that refuses to send unless the rewrite happened |
| A fresh recipient for each test | Create with createAddress(), wait with waitForEmail() |
| Let non-developers read the mail | The "Mailbox only" role |
To click the links in received emails from your E2E tests, see Test invitation and password reset links in E2E tests. Full API details are in the API documentation.