# Messages & Flags

> List and read mail, fetch bodies lazily and change read or flagged state.

---

LLMS index: [llms.txt](/llms.txt)

---

## The message object

```json
{
  "id": 42,
  "accountId": 1,
  "mailboxId": 1,
  "mailbox": "INBOX",
  "uid": 507,
  "messageId": "<abc@example.com>",
  "subject": "Invoice for November",
  "from": [{"name": "Billing", "address": "billing@example.com"}],
  "to": [{"name": "", "address": "someone@qq.com"}],
  "cc": [],
  "date": 1764500000,
  "snippet": "Your invoice is attached…",
  "flags": ["\\Seen"],
  "seen": true,
  "flagged": false,
  "answered": false,
  "hasAttachments": true,
  "size": 48211,
  "bodyLoaded": true,
  "attachments": [
    {"id": 7, "filename": "invoice.pdf", "contentType": "application/pdf", "size": 48211}
  ]
}
```

List endpoints return summaries (no bodies). Opening a message adds:

| Field | Description |
| --- | --- |
| `bodyText` | Plain-text body; derived from HTML when the mail has no text part |
| `bodyHtml` | HTML body, when present |
| `bodyError` | Transient message set when a body fetch failed |
| `attachments` | Stored attachment metadata (`id`, `filename`, `contentType`, `size`); present when the message has attachments and they are cached locally |

Passwords aside, `bodyText`/`bodyHtml` are omitted when empty. Attachment
payloads are never inlined into the message object — download them through
`GET /api/attachments/{id}`.

## List messages in a mailbox

`GET /api/mailboxes/{id}/messages`

| Query | Default | Description |
| --- | --- | --- |
| `limit` | `50` | Page size, maximum `200` |
| `offset` | `0` | Skip this many messages |
| `q` | — | Case-insensitive substring match on subject, snippet or body text |
| `unseen` | — | `1` or `true` returns only unread messages |
| `since` | — | Unix seconds; only messages dated at or after this value |

```bash
curl 'http://127.0.0.1:8787/api/mailboxes/1/messages?limit=20&offset=0'
curl 'http://127.0.0.1:8787/api/mailboxes/1/messages?unseen=1'
```

Messages are ordered newest first (`date DESC, id DESC`). The list reads only
the local cache — it does not contact IMAP and does not mark anything as read.

## Read one message

`GET /api/messages/{id}`

```bash
curl http://127.0.0.1:8787/api/messages/42
```

If the body was not cached during sync, Pigeon selects the mailbox read-only,
fetches the single message with `BODY.PEEK` (so it stays unread) and stores the
body before responding. If that fetch fails, the response still contains the
message with `bodyError` set and `bodyLoaded: false`.

Attachments travel with the raw MIME message and are stored locally in the same
fetch. Each file is capped at **25 MB** — larger attachments are skipped (they
do not appear in the `attachments` array and cannot be downloaded), while the
rest of the mail, including its other attachments, is stored normally. Mail
cached before attachment storage existed is backfilled on first open: Pigeon
refetches the raw message once, extracts the attachments and serves them on
every later request without touching the network. A failed backfill is logged
and does not block the message itself.

Error codes: `404` when the message does not exist, `400` for an invalid ID.

## Download an attachment

`GET /api/attachments/{id}`

```bash
curl -OJ http://127.0.0.1:8787/api/attachments/7
```

Responds with the raw file bytes and `Content-Disposition: attachment` carrying
the attachment's **original filename**, so clients save it under that name —
`curl -OJ` and browsers both honor it. Non-ASCII names use RFC 5987
(`filename*=`). Attachments that arrived without a filename are served as
`attachment`. Downloads are served from the local SQLite cache — no IMAP
round-trip — and support `Range` requests.

| Status | Meaning |
| --- | --- |
| `400` | Invalid attachment ID |
| `404` | Attachment not found (or not cached yet — open its message first) |

## Change flags

`POST /api/messages/{id}/flags`

```bash
# Mark read and star
curl -X POST http://127.0.0.1:8787/api/messages/42/flags \
  -H 'Content-Type: application/json' \
  -d '{"seen": true, "flagged": true}'

# Mark unread
curl -X POST http://127.0.0.1:8787/api/messages/42/flags \
  -H 'Content-Type: application/json' \
  -d '{"seen": false}'
```

| Field | Description |
| --- | --- |
| `seen` | Read/unread (`\Seen`) |
| `flagged` | Starred (`\Flagged`) |

Pass at least one field; both are optional pointers, so omitted fields are left
untouched. Pigeon applies the change on the IMAP server first (selecting the
mailbox read-write), then updates the local cache and mailbox counts, and
returns the updated message.

| Status | Meaning |
| --- | --- |
| `400` | Neither `seen` nor `flagged` provided |
| `404` | Message not found |
| `502` | IMAP update failed; nothing changed locally |

Reading mail through the API never marks it as read automatically — clients
should call this endpoint explicitly (the built-in web UI does so when a
message is opened). MCP read tools follow the same rule.

---

Backlinks:

- [Documentation](/docs/)
- [REST API Basics](/docs/api/)
- [Search](/docs/search/)
