# Read mail

> List mailboxes, list and search messages, read bodies, threads and attachments, and change flags, folders and labels.

URL: https://docs.workwae.com/guides/mail

Workwae keeps an index of each account's mail. Read mailboxes, messages and threads from it, open bodies and attachments, and change flags or move messages; every change goes to the provider. The full list of operations is in the [Mail reference](/reference/mail).

## Turn mail off

Mail is on by default when the workspace has the mail module on; without it every mail route answers 403 `plan_module_unavailable`, no mail syncs, and sign-ins ask for no mail scopes (see [Modules](/guides/rate-limits#modules)). Set `mailEnabled` to `false` in the tenant settings, or per account in `settings`, for an account that needs only calendars, contacts or meetings.

- While it is off, no mail syncs and every mail route (mailboxes, messages, search, threads, drafts, sends, mail merges, identities and AI drafts) answers 409 `mail_disabled`. Indexed mail stays but cannot be read.
- New sign-ins through Google or Microsoft ask for no mail scopes. Turning mail back on for an account whose grant lacks them needs a reconnect.
- Changing `mailEnabled` restarts the account's syncs.

| Status | Reason | Meaning |
| --- | --- | --- |
| 409 | `mail_disabled` | The account's mail is off. |
| 403 | `plan_module_unavailable` | The workspace has the mail module off. |

## Read-only and read-and-send accounts

An account connected with [`mailPermissions`](/guides/connect#hosted) `read_send` or `read` cannot change its mailbox; its `mailPermissions` says so. Sync, listing, search, threads, bodies and attachments work as for any account. Cancelling and rescheduling queued sends always work.

| Status | Reason | Meaning |
| --- | --- | --- |
| 409 | `mail_permission_missing` | `read_send` or `read`: updating or deleting messages and threads, creating, changing or deleting mailboxes and drafts, or sending a stored draft. `read`: also queueing a send or a mail merge. |

## Sync window and initial sync

A new account syncs in the background. Until its first mail sync finishes, the account's `initializedAt` is null. The `account.initialized` event announces the end of it with the number of mailboxes and messages indexed; messages from before it are not announced one by one.

- The index covers the account's sync window: the last `syncWindowDays` days (90 by default; `null` keeps the full mailbox history). Set it in the tenant settings or per account in `settings`.
- `bodyStorage` decides whether message content is stored: `none` (the default) reads bodies from the provider on request, `cache` stores a message's content the first time it is read, `full` stores every indexed message.
- Changing `syncWindowDays` or `bodyStorage` restarts the account's syncs. `full` backfills every indexed message; `none` deletes stored content.
- `POST /v1/accounts/{accountId}/sync` wakes a sync now, at most 30 times per account per minute.

Listings cover the sync window only. [Search](#search) can reach older mail through the provider.

## List mailboxes

Mailboxes are the account's folders (Microsoft, IMAP) or labels (Gmail). Find the inbox and other system mailboxes by `role`.

```bash tab="cURL"
curl "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/mailboxes" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
const { items: mailboxes } = await workwae.mailboxes.list({ accountId });
const inbox = mailboxes.find((mailbox) => mailbox.role === "inbox");
```

| Field | Meaning |
| --- | --- |
| `role` | `inbox`, `sent`, `drafts`, `trash`, `junk`, `archive`, `all`, `important`, or `custom` for the user's own. |
| `kind` | `folder`: a message is in exactly one. `label`: a message can carry several (Gmail). |
| `path` | The full name, `/`-separated from the top level. `parentId` names the parent. |
| `totalCount`, `unreadCount` | Message counts, or `null` when the provider reports none. They change without an event. |

## List messages

`GET /v1/accounts/{accountId}/messages` lists messages newest first. Without `mailboxId` it lists every mailbox. Results are paged with `limit` (1 to 200, default 50) and `cursor`; see [Pagination](/guides/pagination).

```bash tab="cURL"
curl "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages?mailboxId=$MAILBOX_ID&unread=true" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
import { paginate } from "@workwae/sdk";

for await (const message of paginate(workwae.messages.list, {
  accountId,
  mailboxId: inbox?.id,
  unread: true,
})) {
  console.log(message.subject, message.from?.address);
}
```

| Filter | Matches |
| --- | --- |
| `mailboxId` | Messages in that mailbox. An id the account does not have answers 422 `unknown_mailbox`. |
| `from`, `to` | Part of an address or display name, case-insensitive. `to` looks at To, Cc and Bcc. |
| `unread`, `flagged`, `hasAttachments` | `true` or `false`. |
| `importance` | `high`, `normal` or `low`. Messages indexed before Workwae read priorities (`importance: null`) match none. |
| `after`, `before` | `receivedAt` at or after, and before. `before` must be later than `after` (400 `invalid_range`). |
| `outboxId` | The copies of one [send](/guides/sending)'s outbox entry: the sent copy, and the received copy of a message sent to the account itself. |
| `internetMessageId` | The message with exactly this Message-ID, as `internetMessageId` shows it: one `<id>`, angle brackets included, compared exactly (case-sensitive). An id longer than 512 characters is looked up by its `sha256:` hash, as it is stored. Something that is not one `<id>` answers 400 `invalid_input`. |

Filters combine. `outboxId` and `internetMessageId` are each served by an index.

Each item is a message summary: addresses, `subject`, `snippet`, `receivedAt`, `isRead`, `isFlagged`, `hasAttachments`, `mailboxIds`, `threadId` and these:

- `direction`: `outbound` when the message is in a `sent` mailbox or its From is the account's address or one of its identities (drafts included); `inbound` otherwise.
- `outboxId` and `reference`: set when the message is a copy of a [send](/guides/sending) made through the API. They stay after outbox retention deletes the entry.
- `categories`: Microsoft categories, or the Gmail inbox tab (`personal`, `social`, `promotions`, `updates`, `forums`).
- `importance`: read from Graph's `importance`, and from the priority headers on Gmail and IMAP, where a message without one reads `normal`.
- `inReplyTo` and `references`: the Message-IDs the message answers, as [Get a message](#get) describes them, so replies to your sends can be matched by `internetMessageId` without reading each message. They come from the index, with no provider call. A message indexed before Workwae kept them reads `null` and `[]` until the provider reports it again or it is read in full, and so does a Microsoft 365 message, whose sync carries no headers, until it is read in full (`GET` on the message, or `include=body`), which gives the ids to the index. Gmail and IMAP messages synced now carry them from the start.

### Bodies in the list

`include=body` makes each item the full message that [Get a message](#get) returns (body, attachments, `bcc`, `replyTo` and `classification`; `inReplyTo` and `references` are on every summary) instead of a summary, so one call reads a page of mail. `include=body,headers` adds each message's `headers`; `headers` alone is a 400 `invalid_input`, as is any other value.

```bash tab="cURL"
curl "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages?outboxId=$OUTBOX_ID&include=body,headers" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
const page = await workwae.messages.list({
  accountId,
  internetMessageId: "<20261103.1@mail.example.com>",
  include: ["body", "headers"],
});
```

- A page holds at most 25 messages, and 25 when `limit` is omitted; a larger `limit` is a 400 `invalid_input`. Without `include=body` the limit stays 1 to 200 (default 50). Filters, ordering and cursors are the same.
- Bodies come from stored content when the account stores bodies, without a provider call (`cache` fetches and stores what it lacks), and from the provider otherwise, five messages at a time.
- The page is all or nothing. If the provider cannot serve one of the messages, the page fails with the error [Get a message](#get) gives for that message (404 `provider_not_found`, 403 `provider_forbidden`, 429 `provider_throttled`, 502 `provider_unavailable`, or an account-state 409), and no partial page is returned. Retry the page, or list without `include=body` and read the messages one by one.

## Search messages

`GET /v1/accounts/{accountId}/search/messages` takes a query `q` (up to 500 characters) and the same filters as the list, except `importance`. Pages hold up to 100 results (default 25).

```bash tab="cURL"
curl --get "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/search/messages" \
  -H "Authorization: Bearer $WORKWAE_API_KEY" \
  --data-urlencode 'q="quarterly report" -draft' \
  --data-urlencode "from=ada@example.com"
```

```ts tab="TypeScript"
const page = await workwae.search.messages({
  accountId,
  q: '"quarterly report" -draft',
  from: "ada@example.com",
});
console.log(page.source, page.items.length);
```

`source` picks where the search runs:

| `source` | Behavior |
| --- | --- |
| `index` | Full-text search in web search syntax (words, `"phrases"`, `OR`, `-word`) over subject, sender and recipient names and addresses, the snippet, and the body text of stored messages. Best matches first. |
| `provider` | Passes `q` to the provider unchanged: Gmail search syntax, Microsoft KQL, or IMAP `SEARCH TEXT`. Newest first. |
| `auto` (default) | Asks the provider when the index cannot cover the request: `after` lies before the sync window, or the index found nothing while it does not hold everything (the account has a sync window, or `bodyStorage` is not `full`). Otherwise, and whenever the provider cannot be asked (inactive account, rate limit, provider error), the index answers. |

- The response names the `source` that answered; `nextCursor` stays with that source. A provider cursor expires after an hour (400 `invalid_cursor`).
- A provider result the index does not hold (a message older than the sync window) has `id: null` and cannot be opened through the API.
- Filters narrow provider results after the provider answers, so a provider page can hold fewer than `limit` results while `nextCursor` continues.
- Provider searches of a Gmail account count against a per-account limit per minute; past it the search answers 429 `rate_limited` with `Retry-After`.
- With `source=provider`, the provider's refusals surface as 400 `provider_rejected` (it rejected `q`), 400 `provider_unsupported`, 403 `provider_forbidden`, and the account-state 409s below.

## Get a message

`GET /v1/accounts/{accountId}/messages/{messageId}` returns the summary fields (`inReplyTo` and `references` among them) plus `bcc`, `replyTo`, `body` (`text` and `html`, each `null` when the message has no such part), `attachments` and `classification`.

```bash tab="cURL"
curl "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages/$MESSAGE_ID" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
const message = await workwae.messages.get({ accountId, messageId });
console.log(message.body.text ?? message.body.html);
```

- With `bodyStorage` `cache` or `full`, the body comes from stored content (`cache` fetches and stores the raw message on a miss), and attachment ids are MIME part numbers such as `1.2`. With `none`, the body is read from the provider.
- Drafts are always read from the provider.
- Replies to your sends can carry an AI `classification`; see [AI](/guides/ai).
- `inReplyTo` is the Message-ID the message answers: its In-Reply-To header, angle brackets kept (the first id when the header lists several), or `null` when it has none. `references` lists the ids of its References header, oldest first, `[]` when it has none. An id longer than 512 characters is reported as its `sha256:` hash, as `internetMessageId` is, and a chain of more than 100 ids keeps the first and the newest 99. A full message always has the real ids; on a summary they are the indexed ones (see [List messages](#list)).
- `?include=headers` adds `headers`: every top-level header field in the order of the message, as `{ name, value }` with the name as written and the value unfolded and decoded as text. Without it the message has no `headers` field. The fields come from the same read as the body (stored content, or the provider); a message stored before Workwae kept its reply ids has them read from the stored raw message instead.

Errors specific to reading a message (the same apply to the downloads below):

| Status | Reason |
| --- | --- |
| 404 | `message_not_found` (the message does not exist), `provider_not_found` (the provider no longer has it) |
| 409 | `account_paused`, `account_disabled`, `account_reauth_required`, `account_misconfigured`: the provider is needed and the account cannot reach it |
| 403 | `provider_forbidden`: the provider refused to serve the message |
| 429, 502 | `provider_throttled`, `provider_unavailable`, with `Retry-After` when the provider named a wait |

## Download attachments and raw messages

Attachments are listed on the message with `id`, `filename`, `contentType`, `size`, `isInline` and `contentId`. Download one by id, or the whole message as RFC 5322 (`message/rfc822`). Both are served from stored content when the account stores bodies.

```bash tab="cURL"
curl -o invoice.pdf \
  "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages/$MESSAGE_ID/attachments/$ATTACHMENT_ID" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"

curl -o message.eml \
  "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages/$MESSAGE_ID/raw" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
const file = await workwae.messages.attachment({ accountId, messageId, attachmentId });
const bytes = new Uint8Array(await file.arrayBuffer());

const eml = await workwae.messages.raw({ accountId, messageId });
```

The SDK returns a `File`. An attachment id the stored message does not have is asked of the provider, which answers 404 `provider_not_found` when it has none either.

## Threads

A thread groups a conversation's messages. Its `id` is the provider's thread id, the `threadId` on each message.

- `GET /v1/accounts/{accountId}/threads` lists threads by latest activity, newest first (`limit` up to 100, default 25). With `mailboxId`, it lists threads with a message in that mailbox, ordered by their latest message there; each summary still describes the whole thread.
- `GET /v1/accounts/{accountId}/threads/{threadId}` returns the thread with its messages, oldest first.

```bash tab="cURL"
curl "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/threads/$THREAD_ID" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
const thread = await workwae.threads.get({ accountId, threadId });
for (const item of thread.messages) console.log(item.from?.address, item.snippet);
```

Thread summaries carry `participants`, `messageCount`, `unreadCount`, `isFlagged`, `hasAttachments`, `hasDrafts`, `mailboxIds`, `latestMessageId` and `lastMessageAt`. Both listings cover the sync window.

## Mark read, flag and move

`PATCH /v1/accounts/{accountId}/messages/{messageId}` changes flags and mailboxes, and returns the updated summary.

```bash tab="cURL"
curl -X PATCH "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages/$MESSAGE_ID" \
  -H "Authorization: Bearer $WORKWAE_API_KEY" \
  -H "content-type: application/json" \
  -d '{ "isRead": true, "isFlagged": true }'
```

```ts tab="TypeScript"
await workwae.messages.update({ accountId, messageId, isRead: true, isFlagged: true });
```

To move a message, name the target mailbox:

```bash tab="cURL"
curl -X PATCH "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages/$MESSAGE_ID" \
  -H "Authorization: Bearer $WORKWAE_API_KEY" \
  -H "content-type: application/json" \
  -d '{ "mailboxId": "'"$ARCHIVE_ID"'" }'
```

```ts tab="TypeScript"
const archive = mailboxes.find((mailbox) => mailbox.role === "archive");
await workwae.messages.update({ accountId, messageId, mailboxId: archive!.id });
```

| Field | Effect |
| --- | --- |
| `isRead`, `isFlagged` | Set or clear the flag. |
| `mailboxId` | Moves the message there. On Gmail it replaces the message's folder-like labels. |
| `addMailboxIds`, `removeMailboxIds` | Add or remove Gmail labels, up to 50 each. A mailbox that is not a label answers 400 `mailbox_not_label`. |

- A mailbox id the account does not have answers 422 `unknown_mailbox`, with the ids in `data.ids`.
- Provider refusals answer 400 `provider_rejected` or `provider_unsupported`, 403 `provider_forbidden`, 404 `provider_not_found` or 409 `provider_conflict`.

`PATCH /v1/accounts/{accountId}/threads/{threadId}` takes `isRead` and `mailboxId` and applies them to every message of the thread, skipping those already in that state. A failure part way leaves the messages done so far changed; repeat the call to finish the rest.

## Delete messages

`DELETE /v1/accounts/{accountId}/messages/{messageId}` moves the message to Trash. `?permanent=true` deletes it outright.

```bash tab="cURL"
curl -X DELETE "https://api.workwae.com/v1/accounts/$ACCOUNT_ID/messages/$MESSAGE_ID?permanent=true" \
  -H "Authorization: Bearer $WORKWAE_API_KEY"
```

```ts tab="TypeScript"
await workwae.messages.delete({
  params: { accountId, messageId },
  query: { permanent: true },
});
```

`DELETE /v1/accounts/{accountId}/threads/{threadId}` moves every message of the thread that is not already there to Trash. Like the thread update, repeating it after a partial failure finishes the rest.

## Manage folders and labels

`POST /v1/accounts/{accountId}/mailboxes` creates a folder (Microsoft, IMAP) or a label (Gmail) from a `name` and an optional `parentId`. A nested Gmail label is named with `/` between parent and child.

```ts tab="TypeScript"
const projects = await workwae.mailboxes.create({ accountId, name: "Projects" });
await workwae.mailboxes.update({ accountId, mailboxId: projects.id, name: "Clients" });
```

- `PATCH` renames or moves a mailbox; `parentId: null` moves it to the top level.
- System and special-use mailboxes (Inbox, Sent, Drafts, Trash, Junk, Archive, Gmail system labels) cannot be renamed, moved or deleted: 409 `mailbox_protected`.

Deleting a mailbox depends on the provider:

| Provider | `DELETE` removes |
| --- | --- |
| Microsoft | The folder and the messages in it. A folder with child folders is refused (`provider_rejected`). |
| Gmail | The label, taken off its messages, which stay. |
| IMAP | The mailbox and its messages. A mailbox with child mailboxes is refused by the server (`provider_rejected`). |

The index drops the mailbox at once; the messages it held leave on the next sync, as for a deletion made elsewhere.

## Follow changes

Every change is announced as an event, whether it was made in another mail client or through the API (an API change shows in the index at once and is announced once):

- `mailbox.created`, `mailbox.updated` (renamed, moved or given another role) and `mailbox.deleted`.
- `message.created`: received, sent, drafted, or moved in from outside the sync window.
- `message.updated`: read, flagged, moved, or linked to the outbox entry it is a copy of; `changes` names the fields.
- `message.deleted`: carries the message and its mailboxes as last indexed.

Payloads are in [Mail events](/guides/events/mail). Receive them with [webhooks](/guides/webhooks) or poll the [change feed](/guides/changes).

- [Send mail](https://docs.workwae.com/guides/sending): Send, reply and schedule from the same account.
- [Mail events](https://docs.workwae.com/guides/events/mail): Payloads for message and mailbox changes.
