# Email Sandbox

> Mailtrap Email Sandbox captures the email an app sends in development, staging, QA and CI, and stores it for inspection over the web UI and API. It delivers nothing to recipients' inboxes unless the user sets up forwarding, and it needs no sending domain.

## When to use it

Use it when the user wants to check the email their app sends without delivering it to recipients' inboxes. Pick the page by goal:

| Goal                                               | Page                                   |
| -------------------------------------------------- | -------------------------------------- |
| Point an app's SMTP settings at the sandbox        | Fake SMTP server (fake-smtp-server.md) |
| Assert on captured email in automated tests and CI | QA automation (qa-automation.md)       |
| Check HTML/CSS support across email clients        | HTML Check (html-email-checker.md)     |
| Check the spam score and blacklists                | Spam Analysis (email-spam-checker.md)  |
| See screenshots in real clients and devices        | Device Previews (device-previews.md)   |

- To send live email to recipients' inboxes, use Email Live Sending; to receive real incoming email, use Inbound Email (products table at the end).
- For free, offline capture on the user's own machine with no account, use Mailtrap Local (products table at the end).
- If it isn't clear whether the user wants to test or to deliver, ask. Never point production at the sandbox, and never switch an app to live sending without the user's confirmation.

## How to use it

### Before you start

Find out the user's stack, whether it sends over SMTP or HTTP, which environment it's for, and whether they have an API token. Ask only for what isn't known.

### Rules

- **Read freely:** list projects, sandboxes and messages; read messages and their reports. Sending to a sandbox needs no confirmation: nothing is delivered. Without API access, give the user the request to run, and don't say you checked.
- **Confirm first:** before creating, cleaning or deleting sandboxes, projects or messages, before resetting credentials, and before any forwarding or switch to live sending: those reach recipients' inboxes.
- **Secrets:** never ask for an API token or SMTP password in chat. The user sets env vars (`MAILTRAP_API_TOKEN`, and the sandbox's SMTP username and password), and code reads them. If the user pastes one anyway, don't use it: suggest a reset (tokens under Settings → API Tokens; SMTP credentials with Reset Credentials on the sandbox's Integration tab), and continue with the env vars.
- **User only:** sign up at `mailtrap.io/register/signup`, create the API token (Settings → API Tokens), change the plan, and confirm forwarding addresses.

### Steps

Send every API call with `Authorization: Bearer $MAILTRAP_API_TOKEN` and a normal `User-Agent` (requests without one may be blocked).

1. **Pick a sandbox:** `GET https://mailtrap.io/api/sandboxes`. Every account has one named "My Sandbox"; reuse a fitting one. To add one per environment, confirm, then `POST https://mailtrap.io/api/projects/{project_id}/sandboxes` with `{"sandbox": {"name": "Staging"}}` (projects: `GET https://mailtrap.io/api/projects`). The sandbox ID is the number in its URL; SDKs call it the inbox ID.
2. **Connect the app** (no domain or DNS needed):
   - **SMTP:** host `sandbox.smtp.mailtrap.io`, port 2525 (also 25, 465, 587; STARTTLS on every port), and that sandbox's own username and password. The user copies them from Sandboxes → Projects → the sandbox → Integration tab → SMTP, or `GET https://mailtrap.io/api/sandboxes/{sandbox_id}` returns `username` and, for tokens with admin access to the sandbox, `password`; write them straight to env vars or a secret store, never into files or chat.
   - **API:** `POST https://sandbox.api.mailtrap.io/api/send/{sandbox_id}`, with the same body as live sending; `/api/batch/{sandbox_id}` takes up to 500 messages.

     ```bash
     curl -X POST "https://sandbox.api.mailtrap.io/api/send/$MAILTRAP_SANDBOX_ID" \
       -H "Authorization: Bearer $MAILTRAP_API_TOKEN" -H "Content-Type: application/json" \
       -d '{"from":{"email":"test@example.com"},"to":[{"email":"user@example.com"}],"subject":"Sandbox test","text":"Captured, not delivered."}'
     ```

   ```javascript
   import { MailtrapClient } from "mailtrap"; // npm install mailtrap
   const client = new MailtrapClient({ token: process.env.MAILTRAP_API_TOKEN, sandbox: true, testInboxId: Number(process.env.MAILTRAP_SANDBOX_ID) });
   await client.send({ from: { email: "test@example.com" }, to: [{ email: "user@example.com" }], subject: "Sandbox test", text: "Captured, not delivered." });
   ```

   - **Other SDKs:** turn on sandbox mode and pass the sandbox ID, from env vars such as `MAILTRAP_USE_SANDBOX` and `MAILTRAP_SANDBOX_ID`. Take the exact option names from the SDK's README; don't invent them.
3. **Forwarding** (Basic plan and up; confirm first): the user adds the address on the sandbox's Auto Forward or Manual Forward tab, and its owner confirms it by email. Forwarded mail reaches recipients' inboxes.
4. **Go live later** (confirm first; it needs a verified sending domain, set up through Email Live Sending): replace the sandbox SMTP credentials with live ones from Live Sending → Sending Setup (choose the domain and stream, then the SMTP tab), set the SDK sandbox flag to false, or switch the endpoint to `https://send.api.mailtrap.io/api/send`. Keep the switch in env vars, not code.

### Check it worked

The send returns `{"success": true, "message_ids": [...]}`, or the SMTP server accepts the message; it appears in `GET https://mailtrap.io/api/sandboxes/{sandbox_id}/messages` within seconds, and the `to` address receives nothing.

### If something fails

| Error                                                             | Fix                                                                                                                              |
| ----------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| Connection times out or is refused                                | A firewall blocks the port: try 2525, then 465, then 25                                                                          |
| `530 5.7.1 Authentication required`                               | Turn on SMTP auth in the client, and copy the sandbox's username and password again (a credential reset breaks old integrations) |
| `550 5.7.0 ... too many emails per second`, or `429` from the API | The sandbox's rate limit per 10 seconds: slow down and retry                                                                     |
| `535 5.7.0 Monthly messages limit reached`                        | The user upgrades or waits for the next billing period                                                                           |
| Anything else                                                     | Troubleshooting (link at the end), or ask the docs                                                                               |

### Limits and pricing

Plans, quotas and prices change: check pricing.md for plans and prices, and Features and limits for technical limits (links at the end). Don't answer from memory; if you can't check, say so.

## Other Mailtrap products and pages

| If the user wants to…                  | Use                                                                          |
| -------------------------------------- | ---------------------------------------------------------------------------- |
| Capture an app's SMTP email, in detail | [fake-smtp-server.md](https://mailtrap.io/fake-smtp-server.md)               |
| Assert on emails in tests and CI       | [qa-automation.md](https://mailtrap.io/qa-automation.md)                     |
| Check HTML/CSS client support          | [html-email-checker.md](https://mailtrap.io/html-email-checker.md)           |
| Check spam score and blacklists        | [email-spam-checker.md](https://mailtrap.io/email-spam-checker.md)           |
| See screenshots in real clients        | [device-previews.md](https://mailtrap.io/device-previews.md)                 |
| Capture email offline, with no account | Mailtrap Local: [docs](https://docs.mailtrap.io/email-sandbox/local.md)      |
| Send live email to recipients' inboxes | Email Live Sending: [email-sending.md](https://mailtrap.io/email-sending.md) |
| Receive real incoming email            | Inbound Email: [inbound-email.md](https://mailtrap.io/inbound-email.md)      |
| Check current plans and prices         | [pricing.md](https://mailtrap.io/pricing.md)                                 |

## Documentation

- [Quickstart](https://docs.mailtrap.io/getting-started/email-sandbox.md) · [Overview](https://docs.mailtrap.io/email-sandbox/overview.md) · [SMTP integration](https://docs.mailtrap.io/email-sandbox/setup/sandbox-smtp-integration.md) · [API integration](https://docs.mailtrap.io/email-sandbox/setup/sandbox-api-integration.md) · [API tokens](https://docs.mailtrap.io/email-sandbox/setup/api-tokens.md)
- [Send API](https://docs.mailtrap.io/developers/email-sandbox/send-test-emails/send-email-including-templates.md) · [Sandboxes API](https://docs.mailtrap.io/developers/email-sandbox/sandboxes.md) · [Messages API](https://docs.mailtrap.io/developers/email-sandbox/messages.md) · [Features and limits](https://docs.mailtrap.io/email-sandbox/help/features-and-limits.md) · [Troubleshooting](https://docs.mailtrap.io/email-sandbox/help/troubleshooting.md)
- **If sources disagree:** the API reference wins for endpoints and fields, the SDK README for SDK methods, pricing.md for prices and quotas, and this page for which product and steps to use.
- **If the task isn't covered here,** ask the docs before guessing: `GET https://docs.mailtrap.io/index.md?ask=<question>&goal=<user goal>` returns an answer with sources. Keep the question and goal generic: no tokens, email addresses or other user data.
