Outlook 邮件 / 日历管理
前置条件
- Windows,且本机已安装 Outlook 桌面客户端并至少启动过一次
- 需要
pywin32。不要自己去猜哪个 Python 装了 pywin32,统一用启动器:
python <skill目录>/scripts/run.py <子命令> [参数]
run.py 会检测当前解释器是否可用,不行就自动切换到已安装 pywin32 的解释器并重跑;
都不可用时会打印修复建议。不要硬编码具体 Python 版本或解释器路径。
第一步永远是自检
python scripts/run.py doctor --json
返回解释器版本、Outlook 版本、账户列表与默认账户。
账户相关的一切(发件账户、--account 取值)都从这里的输出取,不要从文档里抄邮箱地址。
python scripts/run.py accounts --json # 只列账户
输出约定
- stdout 只放数据:加
--json时 stdout 是可直接json.loads的纯 JSON,日志/进度全部走 stderr。 - 不加
--json时,人类可读内容输出到 stderr(终端里照常可见)。 - 出错时退出码 1,需要二次确认时退出码 2。
命令速查
发送
# 预览:只存草稿,不真的发出去(建议先跑这个)
python scripts/run.py send --to "someone@example.com;other@example.com" --subject "主题" --body "<p>正文</p>" --dry-run
# 真正发送
python scripts/run.py send --to "someone@example.com" --subject "主题" --body "正文" \
--cc "cc@example.com" --attachments "/path/a.pdf" "/path/b.xlsx" --account "<账户邮箱>"
--body 默认按 HTML 解析(--plain 切纯文本);--attachments 可传多个;
不传 --account 用默认账户。发送是对外动作,未经用户明确同意不要去掉 --dry-run。
读取 / 搜索
python scripts/run.py read --limit 10 --json
python scripts/run.py read --unread --json
python scripts/run.py read --since 2026-10-01 --until 2026-10-31 --json
python scripts/run.py read --sender "张三" --subject "报告" --limit 5 --json # 条件之间是 AND
python scripts/run.py read --folder "收件箱/bugsystem" --limit 5 --json # 支持子文件夹路径
python scripts/run.py read --folder "已发送" --limit 5 --json
python scripts/run.py search --keyword "关键词" --limit 20 --json # --search-body 连正文一起搜
返回字段:entry_id / subject / sender / sender_email / to / cc / received_time / unread / has_attachments / attachment_count / preview。
sender_email 已把 Exchange 的 X500 地址解析成真实 SMTP 地址。
--scan N(默认 500/1000)限制最多扫描多少封,按时间倒序扫描。
邮件量大且要捞很久以前的邮件时,配合 --since/--until 缩小范围,而不是无脑调大 --scan。
单封详情 / 下载附件
python scripts/run.py detail --subject "关键词" --json
python scripts/run.py detail --entry-id "<entry_id>" --save-attachments "D:/tmp/att" --json
--save-attachments 会创建目录,同名文件自动加序号,不覆盖。
删除 / 移动 / 标记
python scripts/run.py delete --subject "关键词" --dry-run # 只看会删哪些
python scripts/run.py delete --subject "关键词" --yes # 真删(进"已删除邮件")
python scripts/run.py move --subject "关键词" --to-folder "已处理" --dry-run
python scripts/run.py move --subject "关键词" --to-folder "已处理" --yes
python scripts/run.py mark-read --entry-id "<id>" # mark-unread 反之
规则:
- 必须给
--subject/--sender/--entry-id至少一个,没有"删全部"这种操作,这是故意的。 - 不加
--yes只做预览并列出待处理邮件,不会真删/真移。 - 多个条件同时给出时是 AND,不是 OR。
--entry-id可重复传多次,用于精确操作 search/read 拿到的具体邮件。
日历
python scripts/run.py calendar --today --json
python scripts/run.py calendar --months-back 1 --months-forward 2 --json
python scripts/run.py calendar --since 2026-11-01 --until 2026-11-30 --json
python scripts/run.py create-meeting --subject "周会" --start "2026-11-05 10:00" --end "2026-11-05 11:00" \
--location "会议室 A" --attendees "someone@example.com" --optional "optional@example.com" --dry-run
python scripts/run.py cancel-meeting --subject "周会" --days 60 # 不加 --yes 只预览
会议时间一律按本机时区理解,格式 YYYY-MM-DD HH:MM。
创建会议同样支持 --dry-run(只写日历不发邀请)。
文件夹
python scripts/run.py folders --depth 2 --json
排错
| 现象 | 处理 |
| --- | --- |
| 没有找到安装了 pywin32 的 Python 解释器 | pip install pywin32,或用装了 pywin32 的解释器直接跑 scripts/outlook_manager.py |
| 无法连接本机 Outlook | 打开 Outlook 桌面客户端确认已登录,再重试 |
| 未找到账户 xxx | 先用 accounts --json 看实际账户名,不要凭印象填 |
| 未找到文件夹 xxx | 用 folders --json 查真实文件夹名;大小写不敏感,/ 可表示层级 |
| 结果为空但你确信有邮件 | 检查 --since/--until、--folder 是否正确;必要时调大 --scan |
维护须知(改动前必读)
这三条是本机实测踩过的坑,改动代码时不要退回旧写法:
- 日期过滤必须用 DASL(UTC),例如
@SQL="urn:schemas:httpmail:datereceived" >= '2026-09-30 16:00:00'。 用 JET 的[ReceivedTime] >= '2026-10-01 00:00:00'在中文系统下静默返回 0 条, 必须带 AM/PM('2026/10/01 12:00 AM')才对。代码里 DASL 优先、JET 兜底。 - 日历开启
IncludeRecurrences后Items.Count恒为 2147483647, 用它做range()会直接卡死。必须GetFirst()/GetNext()遍历(见iter_items), 并且IncludeRecurrences要设在Sort之前。 - 不要把
datetime对象直接赋给Appointment.Start/End。pywin32 会先按本机时区转 UTC, Outlook 又当成本地时间,东八区下 10:00 会变成 02:00。必须赋字符串(%Y-%m-%d %H:%M:%S)。
另外:日志一律走 stderr,stdout 只用于 --json 数据——不要把 print() 混进返回路径,
否则 Agent 解析 JSON 会直接失败。
依赖
pywin32- Windows + 已配置好的 Outlook 桌面客户端

Scan to join WeChat group