# Mailbox Rules

Source: https://molted.email/docs/rules

Label or archive new inbound conversations automatically, based on sender or subject.

Mailbox rules automatically label or archive new inbound conversations. Each rule defines a condition (what to match) and an action (what to do). Rules run in priority order and can optionally stop processing further rules when matched.

Rules run once per conversation: on the inbound message that starts a new thread. A reply that joins an existing thread is not evaluated again. Every enabled rule of the thread's mailbox whose condition matches applies its action, in `position` order, until one with `stopProcessing: true` matches.

Rules are managed with your Bearer token, through the mailbox service (`/v1/agent/rules` on `https://mailbox.molted.email`, which fills in your `tenantId` from the key; on `api.molted.email` every call carries it), or the CLI (`molted rules`). The MCP server has no rule tools.

## Create a rule

```
POST https://mailbox.molted.email/v1/agent/rules
```

```bash title="curl"
curl -X POST "https://mailbox.molted.email/v1/agent/rules" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "mailboxId": "MAILBOX_UUID",
    "name": "Archive newsletters",
    "conditionType": "sender_domain",
    "conditionOperator": "ends_with",
    "conditionValue": "newsletter.example.com",
    "actionType": "auto_archive",
    "enabled": true,
    "stopProcessing": false
  }'
```

```json title="Response"
{
  "id": "rule_uuid",
  "mailboxId": "MAILBOX_UUID",
  "name": "Archive newsletters",
  "conditionType": "sender_domain",
  "conditionOperator": "ends_with",
  "conditionValue": "newsletter.example.com",
  "actionType": "auto_archive",
  "actionValue": null,
  "enabled": true,
  "position": 0,
  "stopProcessing": false
}
```

### Condition types

| Type | Matches against |
| - | - |
| `sender_domain` | The domain portion of the sender's email address. |
| `sender_email` | The full sender email address. |
| `subject` | The message subject line. |

### Condition operators

Every operator ignores case.

| Operator | Description |
| - | - |
| `equals` | The whole value matches exactly. |
| `contains` | Substring match. |
| `matches_pattern` | A SQL `LIKE`-style pattern over the whole value: `%` matches any run of characters, `_` any one character (for example `%invoice%` or `support@%.example.com`). It is not a glob: `*` and `?` are literal. |
| `ends_with` | Suffix match. |

### Actions

| Action | `actionValue` | Description |
| - | - | - |
| `apply_label` | Label ID | Apply a [label](https://molted.email/docs/labels.md) of the same mailbox to the new thread, recorded as applied by the rule (`appliedBy: { "type": "rule" }`). |
| `move_to_folder` | Label ID | The old name of `apply_label`; still works. |
| `auto_archive` | Not required | Archive the new thread immediately (it moves to the `archived` view). |

### Request fields

| Field | Type | Required | Description |
| - | - | - | - |
| `mailboxId` | `string` | Yes | UUID of the mailbox to attach the rule to. |
| `name` | `string` | Yes | Human-readable label for the rule. |
| `conditionType` | `string` | Yes | What to match against. |
| `conditionOperator` | `string` | Yes | How to compare. |
| `conditionValue` | `string` | Yes | The value to compare against. For `matches_pattern`, at most 200 characters and 10 `%` wildcards (a longer pattern is a `400`). Only the first 2,000 characters of the sender or subject are compared. |
| `actionType` | `string` | Yes | What to do when the condition matches. |
| `actionValue` | `string` | For `apply_label` / `move_to_folder` | The label ID. Missing is `400`; a label that isn't in the rule's mailbox is `404`. |
| `enabled` | `boolean` | No | Whether the rule is active. Defaults to `true`. |
| `position` | `integer` | No | Sort order. Lower values run first; rules with the same position run oldest first. Defaults to `0`. |
| `stopProcessing` | `boolean` | No | Stop evaluating further rules when this one matches. Defaults to `true` (on the API and in `molted rules create`); pass `false` / `--no-stop-processing` to let later rules run too. |

Rule names are unique within a mailbox (`409` otherwise).

```bash title="CLI"
molted rules create --mailbox MAILBOX_UUID --name "Archive newsletters" \
  --condition-type sender_domain --condition-operator ends_with \
  --condition-value newsletter.example.com --action-type auto_archive \
  --no-stop-processing
```

## List rules

`mailboxId` is required (`400` without it). Rules come back in evaluation order.

```
GET https://mailbox.molted.email/v1/agent/rules?mailboxId=MAILBOX_UUID
```

```bash title="curl"
curl "https://mailbox.molted.email/v1/agent/rules?mailboxId=MAILBOX_UUID" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

```bash title="CLI"
molted rules list --mailbox MAILBOX_UUID
```

## Get a rule

```
GET https://mailbox.molted.email/v1/agent/rules/:id
```

```bash title="CLI"
molted rules get RULE_UUID
```

## Update a rule

```
PATCH https://mailbox.molted.email/v1/agent/rules/:id
```

```bash title="curl"
curl -X PATCH "https://mailbox.molted.email/v1/agent/rules/RULE_UUID" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"enabled": false}'
```

All fields from the create request except `mailboxId` are optional for updates. Enabling a rule whose label is gone, or that has no `actionValue` for its label action, is `400`.

```bash title="CLI"
molted rules update RULE_UUID --no-enabled
```

## Delete a rule

```
DELETE https://mailbox.molted.email/v1/agent/rules/:id
```

```bash title="curl"
curl -X DELETE "https://mailbox.molted.email/v1/agent/rules/RULE_UUID" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

```bash title="CLI"
molted rules delete RULE_UUID
```

## Reorder rules

Rules are evaluated in ascending `position` order. Use the reorder endpoint to set the evaluation order explicitly.

```
POST https://mailbox.molted.email/v1/agent/rules/reorder
```

```bash title="curl"
curl -X POST "https://mailbox.molted.email/v1/agent/rules/reorder" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "mailboxId": "MAILBOX_UUID",
    "ruleIds": ["rule-uuid-1", "rule-uuid-2", "rule-uuid-3"]
  }'
```

Rules are assigned positions in the order of the `ruleIds` array (first element = position 0). `ruleIds` must list every rule of the mailbox exactly once (`400` otherwise). The response is the mailbox's rules in their new order.

```bash title="CLI"
molted rules reorder --mailbox MAILBOX_UUID --rule-ids rule-uuid-1,rule-uuid-2,rule-uuid-3
```

## Stop processing

When `stopProcessing: true` is set on a rule, evaluation halts after the rule matches. This is useful for high-priority rules that should take exclusive action.

## Label deletion

Deleting a label automatically disables (does not delete) any rule whose `apply_label` (or `move_to_folder`) action targets it. Update the rule to point at a different label, or delete it, once you no longer need it. A matching inbound message is never dropped because of a rule pointing at a deleted label: it still lands in the inbox even if the rule's action can't be applied.

A label applied by a rule is business metadata: it never changes how the message is routed, held or quarantined. A rule labels the thread even when its first message is quarantined or the thread goes to a review queue; the `thread.labeled` event only fires for a thread your agent can see.

A rule read (list, get, create, update) carries `invalidLabel: true` and `invalidLabelReason` when its label can't be applied: `label_missing` (deleted) or `label_in_other_mailbox` (a rule made before a rule had to name a label of its own mailbox). Such a rule matches but applies nothing; point it at a label of its own mailbox.

## Authorization

Creating, updating, deleting and reordering rules need the key's `manage` scope and, for a mailbox-scoped key, `manage` on the mailbox. Reads need `read`. See [Authentication](https://molted.email/docs/authentication.md) for key scoping details.

## Related reading

- [Mailboxes](https://molted.email/docs/mailboxes.md): create and manage mailboxes (a clone copies its rules)
- [Labels](https://molted.email/docs/labels.md): the labels a rule applies
- [Inbound](https://molted.email/docs/inbound.md): how inbound messages are classified and routed
