create_rule
Create automated email rules to organize iCloud Mail by applying actions like move, delete, or mark to messages matching specific filters such as sender, subject, or date.
Instructions
Create a saved rule that applies a specific action to emails matching a set of filters. Rules are stored persistently and can be run on demand or all at once with run_all_rules.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Unique rule name (used to run or delete the rule) | |
| description | No | Optional human-readable description of what the rule does | |
| filters | Yes | Email filters (same as bulk_move/bulk_delete filters: sender, domain, subject, before, since, unread, flagged, larger, smaller) | |
| action | Yes | Action to apply to matching emails |
Implementation Reference
- lib/imap.js:1822-1846 (handler)Implementation of the create_rule tool handler.
export function createRule(name, filters, action, description = '') { const data = readRules(); if (data.rules.find(r => r.name === name)) { throw new Error(`Rule '${name}' already exists. Delete it first to update it.`); } const validActions = ['move', 'delete', 'mark_read', 'mark_unread', 'flag', 'unflag']; if (!validActions.includes(action.type)) { throw new Error(`Invalid action type '${action.type}'. Must be one of: ${validActions.join(', ')}`); } if (action.type === 'move' && !action.targetMailbox) { throw new Error(`Action type 'move' requires targetMailbox`); } const rule = { name, description, filters, action, createdAt: new Date().toISOString(), lastRun: null, runCount: 0, }; data.rules.push(rule); writeRules(data); return rule; }