# Inbound Email

> Mailtrap Inbound Email receives email for an app or AI agent: it accepts messages for an inbox address, parses them into JSON, and serves them through the API and webhooks, with replies and forwards through the API. Part of Email Live Sending.

## When to use it

Use it when the user's app or agent must receive email at an address, read the parsed message, and optionally reply or forward.

| Address type          | What it receives                                                                                                              | Needs                                                                                       |
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| Mailtrap-hosted inbox | One generated address under `@inbound-mailtrap.io`                                                                            | An API token only; no domain, no DNS                                                        |
| Custom-domain inbox   | Every address on the user's domain in one catch-all inbox (`*@example.com`): `support@` and `billing@` land in the same inbox | A verified sending domain with inbound enabled and an MX record; the custom domains feature |

- Folders, inboxes and messages are managed in the dashboard (Inbound → Create Folder, Create Inbox), through the API, an SDK or the MCP server.
- To split mail by address, read `to` on each message and route in code, or use separate Mailtrap-hosted inboxes. The docs don't say whether one domain can have several inboxes: don't promise it.
- To capture test email the user's own app sends, use Email Sandbox (table at the end); its inboxes and IDs are different.
- If it isn't clear which address type the user needs, ask.

## How to use it

### Before you start

Find out what the app does with received email, whether it needs its own domain, whether it replies or forwards, where webhooks go, and whether the user has an API token. Ask only for what isn't known.

### Rules

- **Read freely:** list folders, inboxes, webhooks, messages and threads; read a message. Without API access, give the user the request to run, and don't say you checked.
- **Confirm first:** before creating or deleting folders, inboxes and webhooks, deleting messages, or enabling inbound on a domain. Reply, reply all and forward send live email: confirm the recipients and content right before each one, unless the current request asks for that exact message.
- **Automatic rules** (answering every email, forwarding matches) need the user's explicit approval of the trigger, recipients and text; then the app's code sends without per-message confirmation. Write that code so it skips no-reply and mailer-daemon senders, the inbox's own address or domain, and messages with an `Auto-Submitted` header other than `no`, and handles each message ID once.
- **Secrets:** never ask for a token or webhook signing secret in chat. The user sets env vars (`MAILTRAP_API_TOKEN`, and for example `MAILTRAP_WEBHOOK_SECRET`), and code reads them. If the user pastes one anyway, don't use it: suggest a reset (tokens under Settings → API Tokens, signing secrets in the webhook's details on the Webhooks page), and continue with the env var.
- **User only:** sign up, create the first API token with account-level access (Settings → API Tokens), and, for a custom domain, verify it and add the MX record.

### Steps

Send every request with `Authorization: Bearer $MAILTRAP_API_TOKEN`, `Content-Type: application/json` and a normal `User-Agent`.

1. **Check, then create a folder:** `GET https://mailtrap.io/api/inbound/folders`. Reuse a fitting one, or confirm, then `POST https://mailtrap.io/api/inbound/folders` with `{"name": "Support"}`.
2. **Check, then create the inbox:** `GET https://mailtrap.io/api/inbound/folders/{folder_id}/inboxes`. If none fits, confirm, then `POST` to the same URL with `{"name": "Support tickets"}`. The response has `id` and `address`.
   - **Custom domain:** enable inbound on the verified domain (confirm first) with `PATCH https://mailtrap.io/api/domains/{id}` and `{"domain": {"inbound_enabled": true}}`. The user adds the MX record (the `dns_records` item with `key: inbound_mx`); mail is accepted once `inbound_verified` is `true`. Then create the inbox with `"domain_id": <id>` (from `GET https://mailtrap.io/api/domains`).
   - **MX:** the record changes where the domain's mail is delivered. If the domain already receives mail elsewhere, ask the user first, and suggest a subdomain verified as its own sending domain.
3. **Get notified, or poll:**
   - **Webhook** (check `GET https://mailtrap.io/api/webhooks` first, then confirm): `POST https://mailtrap.io/api/webhooks` with `{"webhook": {"url": "<user's endpoint>", "webhook_type": "inbound_receiving", "inbound_inbox_id": <inbox id>}}`. The URL must use port 443 or 80. The response returns `data.signing_secret` only on creation: write it straight to the env var or secret store, never print it.
   - **Handler:** loop over the `events` array; each `inbound.message_received` event carries the inbox and message IDs, not the body: fetch the message in step 4. Verify `Mailtrap-Signature` (hex HMAC-SHA256 of the raw body with the signing secret, compared in constant time), and return `200` within 30 seconds.
   - **Polling:** `GET https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages`, newest first; pass `last_id` from the response for the next page.
4. **Read the message:** `GET https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{id}` returns `from`, `to`, `subject`, `thread_id`, HTML and text bodies, and URLs for the raw `.eml` and each attachment. The URLs expire after one hour: download what's needed, or fetch the message again.
5. **Reply or forward** (confirm first, as in Rules): `POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{id}/reply`, `/reply_all` or `/forward`, with `text` and/or `html`. A forward needs `to`, an array of `{"email", "name"}`. A Mailtrap-hosted inbox always sends from its own address (passing `from` returns `400`); a custom-domain inbox needs `from` on its domain.

### Check it worked

- The user emails the inbox address; the message appears in the list-messages response, and the webhook receives `inbound.message_received` with a valid signature.
- A reply or forward returns `201`.

### If something fails

| Error                                         | Fix                                                                                                                                               |
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `401 Unauthorized`, `403 Forbidden` on create | The user checks under Settings → API Tokens that the token has account-level access                                                               |
| `400` on reply or forward                     | Remove `from` for a Mailtrap-hosted inbox, or put it on the inbox's domain                                                                        |
| No email arrives on a custom domain           | `GET https://mailtrap.io/api/domains/{id}`: `inbound_enabled` and `inbound_verified` must be `true`; if not, the user checks the MX record        |
| Webhook events stop                           | Failed deliveries are retried, then the webhook is paused; the user resumes it on the Webhooks page. Messages stay in the inbox: poll to catch up |
| Anything else                                 | The docs (links at the end), or ask the docs                                                                                                      |

### Limits and pricing

Received and sent emails share the plan's quota, and messages are kept only for the plan's retention window: fetch what the app needs promptly. For plans, quotas and prices, check pricing.md (link at the end); don't answer from memory, and if you can't check, say so.

## Other Mailtrap products and pages

| If the user wants to…                                          | Use                                                                     |
| -------------------------------------------------------------- | ----------------------------------------------------------------------- |
| Set up an agent's inbox with MCP tools or the CLI              | Agent Inbox: [agent-inbox.md](https://mailtrap.io/agent-inbox.md)       |
| Send email that doesn't start from a received message          | Email API: [email-api.md](https://mailtrap.io/email-api.md)             |
| Capture test email the user's app sends, without delivering it | Email Sandbox: [email-sandbox.md](https://mailtrap.io/email-sandbox.md) |
| Check current plans and prices                                 | [pricing.md](https://mailtrap.io/pricing.md)                            |

## Documentation

- [Inbound Email overview](https://docs.mailtrap.io/inbound-email/overview.md) · [Receiving emails](https://docs.mailtrap.io/inbound-email/receiving-emails.md) · [Inbound webhooks](https://docs.mailtrap.io/inbound-email/webhooks.md) · [Webhook signatures and retries](https://docs.mailtrap.io/email-api-smtp/advanced/webhooks.md)
- [Messages API](https://docs.mailtrap.io/developers/inbound/messages.md) · [Reply API](https://docs.mailtrap.io/developers/inbound/messages/reply-to-a-message.md) · [Forward API](https://docs.mailtrap.io/developers/inbound/messages/forward-a-message.md) · [Threads API](https://docs.mailtrap.io/developers/inbound/threads.md) · [Domain setup](https://docs.mailtrap.io/email-api-smtp/setup/sending-domain.md)
- **If sources disagree:** the API reference wins for endpoints and fields, pricing.md for quotas and prices, 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.
