Skip to main content
Glama
README.md
# snagmail

Local fake SMTP server + MCP server. Your app's outgoing test mail never leaves `localhost`;
OTP codes and verification links are extracted for you and handed to AI agents (MCP) or test suites (REST).
Receive-only: it never sends mail. In-memory, no Docker, no database.

## Quick start

```bash
npx snagmail
```

- SMTP catcher: `127.0.0.1:1025`
- Web UI, REST API, MCP (`/mcp`): `http://127.0.0.1:8025`

Point your app at it:

```env
SMTP_HOST=127.0.0.1
SMTP_PORT=1025
SMTP_SECURE=false
```

## MCP setup

**Stdio** (the client launches snagmail itself; it also starts the SMTP catcher and UI):

```bash
claude mcp add --transport stdio snagmail -- npx -y snagmail --mcp-stdio
```

Claude Code `.mcp.json`, Cursor `.cursor/mcp.json`, Claude Desktop (all use `mcpServers`):

```json
{ "mcpServers": { "snagmail": { "command": "npx", "args": ["-y", "snagmail", "--mcp-stdio"] } } }
```

VS Code `.vscode/mcp.json` (this file uses `servers`, not `mcpServers`):

```json
{ "servers": { "snagmail": { "type": "stdio", "command": "npx", "args": ["-y", "snagmail", "--mcp-stdio"] } } }
```

**HTTP** (run `npx snagmail` yourself, then connect; use this when several windows/agents share one inbox):

```bash
claude mcp add --transport http snagmail http://127.0.0.1:8025/mcp
```

Claude Code `.mcp.json`:

```json
{ "mcpServers": { "snagmail": { "type": "http", "url": "http://127.0.0.1:8025/mcp" } } }
```

Cursor `.cursor/mcp.json`:

```json
{ "mcpServers": { "snagmail": { "url": "http://127.0.0.1:8025/mcp" } } }
```

VS Code `.vscode/mcp.json`:

```json
{ "servers": { "snagmail": { "type": "http", "url": "http://127.0.0.1:8025/mcp" } } }
```

Only one snagmail can own the SMTP port. A second instance exits with an error; connect to the first one over HTTP instead.

## MCP tools

| Tool | Params |
| :-- | :-- |
| `wait_for_email` | `to` (`*` wildcard), `subject_contains`, `timeout_ms` (default 30000, max 300000), `since_timestamp` |
| `list_emails` | `to`, `limit` (default 10) |
| `get_email` | `id`, `format`: `clean_text` \| `html` \| `raw` |
| `clear_inbox` | `to` |

`wait_for_email` returns the **newest matching email already in the inbox**, or waits for a new one.
For repeat tests on the same address, call `clear_inbox` first, pass `since_timestamp`, or use a unique recipient.
`to` matches the SMTP envelope recipient (so BCC works), case-insensitive.

Result: `id, from, to, envelope_to, header_to, cc, subject, extracted_otp, action_links[{type,label,url}], clean_text, received_at`.
Link types: `verify`, `reset_password`, `magic_link`, `unsubscribe`, `other`.

## CLI flags

```
--smtp-port <port>  SMTP port (default 1025)
--http-port <port>  Web UI, REST, /mcp port (default 8025)
--mcp-stdio         Serve MCP over stdio (all logs go to stderr)
--no-ui             Skip the HTTP server (only with --mcp-stdio)
--max-emails <n>    Emails kept in memory (default 200)
```

If the HTTP port is busy in `--mcp-stdio` mode, snagmail warns on stderr and keeps running without the UI.

## REST API

| Endpoint | |
| :-- | :-- |
| `GET /api/v1/emails?to=&search=&limit=` | list (no bodies) |
| `GET /api/v1/emails/wait?to=&subject_contains=&since_timestamp=&timeout_ms=` | long poll; `408` on timeout |
| `GET /api/v1/emails/:id` | full email incl. `html`, `raw` |
| `DELETE /api/v1/emails?to=` | clear all or filtered |
| `WS /ws` | live feed (`email`, `clear`) |

Example (CI): `curl "http://127.0.0.1:8025/api/v1/emails/wait?to=qa@test.local&timeout_ms=10000"`

## Security

Binds to `127.0.0.1` only, validates `Host`/`Origin`, and the UI previews HTML in a script-less sandboxed iframe with remote loads blocked.

## Dev

`npm run dev` · `npm test` · `npm run build`