Email Triage
Scan your IMAP inbox, classify emails into priority categories, and surface the ones that need attention. Uses a local LLM (Ollama) for intelligent classification with a rule-based heuristic fallback when Ollama is unavailable.
Prerequisites
- - Python 3.10+
- IMAP-accessible email account (Gmail, Fastmail, self-hosted, etc.)
- Ollama (optional) — for AI-powered classification. Without it, the script uses keyword-based heuristics that still work well for common patterns.
Categories
| Icon | Category | Description |
|---|
| 🔴 | INLINECODE0 | Outages, security alerts, legal, payment failures, time-critical |
| 🟡 |
needs-response | Business inquiries, questions, action items requiring a reply |
| 🔵 |
informational | Receipts, confirmations, newsletters, automated notifications |
| ⚫ |
spam | Marketing, promotions, unsolicited junk |
Configuration
All configuration is via environment variables:
| Variable | Required | Default | Description |
|---|
| INLINECODE4 | ✅ | — | IMAP server hostname |
| INLINECODE5 |
— |
993 | IMAP port (SSL) |
|
IMAP_USER | ✅ | — | IMAP username / email address |
|
IMAP_PASS | ✅ | — | IMAP password or app-specific password |
|
EMAIL_TRIAGE_STATE | — |
./data/email-triage.json | Path to the JSON state file |
|
OLLAMA_URL | — |
http://127.0.0.1:11434 | Ollama API endpoint |
|
OLLAMA_MODEL | — |
qwen2.5:7b | Ollama model for classification |
Directories Written
- -
EMAIL_TRIAGE_STATE (default: ./data/email-triage.json) — Persistent state file tracking classified emails and surfacing status
Commands
CODEBLOCK0
How It Works
- 1. Connects to IMAP over SSL and fetches unread messages (up to 20 per scan).
- Deduplicates by Message-ID (or a hash of subject + sender as fallback) so emails are never classified twice.
- Classifies each email using Ollama if available, otherwise falls back to keyword heuristics.
- Stores state in a local JSON file — tracks category, reason, and whether the email has been surfaced.
report surfaces only unsurfaced urgent and needs-response emails, sorted by priority.mark-surfaced flags reported emails so they won't appear in future reports.- Auto-prunes state to the most recent 200 entries to prevent unbounded growth.
Integration Tips
- - Heartbeat / cron: Run
scan periodically, then report --json to check for items needing attention. - Agent workflow:
scan → report --json → act on results → mark-surfaced. - Without Ollama: The heuristic classifier handles common patterns (automated notifications, marketing, urgent keywords) well. Ollama adds nuance for ambiguous emails.
- App passwords: If your provider uses 2FA, generate an app-specific password for IMAP access.
邮件分类
扫描您的IMAP收件箱,将邮件按优先级分类,并呈现需要关注的邮件。使用本地大语言模型(Ollama)进行智能分类,当Ollama不可用时,采用基于规则的启发式方法作为后备方案。
前置条件
- - Python 3.10+
- 支持IMAP的邮箱账户(Gmail、Fastmail、自建邮箱等)
- Ollama (可选) — 用于AI驱动的分类。没有它,脚本将使用基于关键词的启发式方法,该方法对常见模式仍然有效。
分类类别
| 图标 | 类别 | 描述 |
|---|
| 🔴 | 紧急 | 故障、安全警报、法律事务、支付失败、时间敏感事项 |
| 🟡 |
需回复 | 业务咨询、问题、需要回复的行动事项 |
| 🔵 | 通知类 | 收据、确认函、新闻通讯、自动通知 |
| ⚫ | 垃圾邮件 | 营销、推广、未经请求的垃圾信息 |
配置
所有配置均通过环境变量进行:
| 变量 | 必需 | 默认值 | 描述 |
|---|
| IMAPHOST | ✅ | — | IMAP服务器主机名 |
| IMAPPORT |
— | 993 | IMAP端口(SSL) |
| IMAP_USER | ✅ | — | IMAP用户名/邮箱地址 |
| IMAP_PASS | ✅ | — | IMAP密码或应用专用密码 |
| EMAIL
TRIAGESTATE | — | ./data/email-triage.json | JSON状态文件路径 |
| OLLAMA_URL | — | http://127.0.0.1:11434 | Ollama API端点 |
| OLLAMA_MODEL | — | qwen2.5:7b | 用于分类的Ollama模型 |
写入目录
- - EMAILTRIAGESTATE(默认:./data/email-triage.json)— 持久化状态文件,记录已分类邮件及其呈现状态
命令
bash
扫描收件箱并分类新的未读邮件
python3 scripts/email/email-triage.py scan
带详细输出的扫描(显示每次分类结果)
python3 scripts/email/email-triage.py scan --verbose
试运行 — 扫描并分类但不保存状态
python3 scripts/email/email-triage.py scan --dry-run
显示未呈现的重要邮件(紧急 + 需回复)
python3 scripts/email/email-triage.py report
与report相同但输出JSON格式(用于程序化使用)
python3 scripts/email/email-triage.py report --json
将已报告的邮件标记为已呈现(使其不再出现)
python3 scripts/email/email-triage.py mark-surfaced
显示分类统计信息
python3 scripts/email/email-triage.py stats
工作原理
- 1. 通过SSL连接IMAP并获取未读邮件(每次扫描最多20封)。
- 去重 — 基于Message-ID(或主题+发件人的哈希值作为后备),确保每封邮件不会被重复分类。
- 分类 — 如果Ollama可用则使用其进行分类,否则回退到关键词启发式方法。
- 存储状态 — 在本地JSON文件中记录类别、原因以及邮件是否已被呈现。
- report — 仅呈现未呈现的紧急和需回复邮件,按优先级排序。
- mark-surfaced — 标记已报告的邮件,使其不会出现在后续报告中。
- 自动清理 — 状态文件保留最近200条记录,防止无限增长。
集成建议
- - 定时任务/cron: 定期运行scan,然后使用report --json检查需要关注的项目。
- 代理工作流: scan → report --json → 处理结果 → mark-surfaced。
- 不使用Ollama: 启发式分类器能很好地处理常见模式(自动通知、营销、紧急关键词)。Ollama为模糊邮件增加更细致的判断。
- 应用密码: 如果您的邮件提供商使用双重认证,请为IMAP访问生成应用专用密码。