apple-mail
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseApple Mail
Apple Mail
Interact with Mail.app via AppleScript and SQLite. Run scripts from:
cd {baseDir}通过AppleScript和SQLite与Mail.app交互。从以下路径运行脚本:
cd {baseDir}Commands
命令
| Command | Usage |
|---|---|
| Refresh | |
| List recent | |
| Search | |
| Fast search | |
| Read email | |
| Delete | |
| Mark read | |
| Mark unread | |
| Send | |
| Reply | |
| List accounts | |
| List mailboxes | |
| 命令 | 使用方式 |
|---|---|
| 刷新 | |
| 列出最近邮件 | |
| 搜索 | |
| 快速搜索 | |
| 读取邮件 | |
| 删除邮件 | |
| 标记为已读 | |
| 标记为未读 | |
| 发送邮件 | |
| 回复邮件 | |
| 列出账户 | |
| 列出邮箱 | |
Refreshing Mail
刷新邮件
Force Mail.app to check for new messages:
bash
scripts/mail-refresh.sh # All accounts, wait up to 10s
scripts/mail-refresh.sh Google # Specific account only
scripts/mail-refresh.sh "" 5 # All accounts, max 5 seconds
scripts/mail-refresh.sh Google 0 # Google account, no waitSmart sync detection:
- Script monitors database message count
- Returns early when sync completes (no changes for 2s)
- Reports new message count:
Sync complete in 2s (+3 messages)
Notes:
- Mail.app must be running (script will error if not)
- does NOT auto-refresh — call
mail-list.shfirst if you need fresh datamail-refresh.sh
强制Mail.app检查新消息:
bash
scripts/mail-refresh.sh # 所有账户,最多等待10秒
scripts/mail-refresh.sh Google # 仅指定账户
scripts/mail-refresh.sh "" 5 # 所有账户,最多等待5秒
scripts/mail-refresh.sh Google 0 # Google账户,不等待智能同步检测:
- 脚本监控数据库消息数量
- 同步完成后提前返回(2秒内无变化)
- 报告新消息数量:
Sync complete in 2s (+3 messages)
注意:
- Mail.app必须处于运行状态(若未运行,脚本会报错)
- 不会自动刷新——如果需要最新数据,请先调用
mail-list.shmail-refresh.sh
Output Format
输出格式
List/search returns:
ID | ReadStatus | Date | Sender | Subject- = unread, blank = read
●
列表/搜索返回格式:
ID | 已读状态 | 日期 | 发件人 | 主题- = 未读,空白 = 已读
●
Gmail Mailboxes
Gmail邮箱
⚠️ Gmail special folders need prefix:
[Gmail]/| Shows as | Use |
|---|---|
| |
| |
| |
| |
Custom labels work without prefix.
⚠️ Gmail特殊文件夹需要添加前缀:
[Gmail]/| 显示名称 | 使用格式 |
|---|---|
| |
| |
| |
| |
自定义标签无需添加前缀。
Fast Search (SQLite)
快速搜索(SQLite)
✨ Now safe even if Mail.app is running — copies database to temp file first.
bash
scripts/mail-fast-search.sh "query" [limit] # ~50ms vs minutesPreviously required Mail.app to be quit. Now works anytime by copying the database to a temp file before querying.
✨ 现在即使Mail.app运行时也能安全使用 —— 先将数据库复制到临时文件。
bash
scripts/mail-fast-search.sh "查询内容" [数量限制] # 约50毫秒,对比常规搜索需数分钟此前需要关闭Mail.app才能使用。现在通过先将数据库复制到临时文件再查询,可随时使用。
Performance Notes
性能说明
Speed by operation:
| Operation | Speed | Notes |
|---|---|---|
| ~50ms | SQLite query, fastest |
| <1s | Simple AppleScript |
| 1-3s | AppleScript, direct mailbox access |
| 1-2s | Creates and sends message |
| ~2s | Position-optimized lookup |
| ~0.5s | Position-optimized lookup |
| ~1.5s | Position-optimized lookup |
Optimization technique:
SQLite provides account UUID and approximate message position. AppleScript jumps directly to that position instead of iterating from the start.
Batch operations supported:
- - Read multiple (separator between each)
mail-read.sh 123 456 789 - - Delete multiple
mail-delete.sh 123 456 789 - - Mark multiple as read
mail-mark-read.sh 123 456 - - Mark multiple as unread
mail-mark-unread.sh 123 456
⚠️ No auto-refresh: Scripts read cached data. Call first if you need latest emails.
mail-refresh.sh各操作速度:
| 操作 | 速度 | 说明 |
|---|---|---|
| 约50毫秒 | SQLite查询,速度最快 |
| <1秒 | 简单AppleScript脚本 |
| 1-3秒 | AppleScript脚本,直接访问邮箱 |
| 1-2秒 | 创建并发送消息 |
| 约2秒 | 基于位置优化的查找 |
| 约0.5秒 | 基于位置优化的查找 |
| 约1.5秒 | 基于位置优化的查找 |
优化技术:
SQLite提供账户UUID和消息近似位置。AppleScript直接跳转到该位置,而非从头开始遍历。
支持批量操作:
- - 读取多封邮件(用空格分隔ID)
mail-read.sh 123 456 789 - - 删除多封邮件
mail-delete.sh 123 456 789 - - 标记多封邮件为已读
mail-mark-read.sh 123 456 - - 标记多封邮件为未读
mail-mark-unread.sh 123 456
⚠️ 无自动刷新: 脚本读取缓存数据。如果需要最新邮件,请先调用
mail-refresh.shManaging Emails
邮件管理
Delete emails:
bash
scripts/mail-delete.sh 12345 # Delete one
scripts/mail-delete.sh 12345 12346 12347 # Delete multipleMark as read/unread:
bash
scripts/mail-mark-read.sh 12345 12346 # Mark as read
scripts/mail-mark-unread.sh 12345 # Mark as unreadBulk operations example:
bash
undefined删除邮件:
bash
scripts/mail-delete.sh 12345 # 删除单封邮件
scripts/mail-delete.sh 12345 12346 12347 # 删除多封邮件标记为已读/未读:
bash
scripts/mail-mark-read.sh 12345 12346 # 标记为已读
scripts/mail-mark-unread.sh 12345 # 标记为未读批量操作示例:
bash
undefinedFind spam emails
查找垃圾邮件
scripts/mail-fast-search.sh "spam" 50 > spam.txt
scripts/mail-fast-search.sh "spam" 50 > spam.txt
Extract IDs and delete them
提取ID并删除
grep "^[0-9]" spam.txt | cut -d'|' -f1 | xargs scripts/mail-delete.sh
undefinedgrep "^[0-9]" spam.txt | cut -d'|' -f1 | xargs scripts/mail-delete.sh
undefinedReading Email Bodies
读取邮件正文
bash
scripts/mail-read.sh 12345 # Single email
scripts/mail-read.sh 12345 12346 12347 # Multiple emails (separated output)Uses position-optimized lookup (~2s per message). Multiple emails are separated by with a summary at the end.
========bash
scripts/mail-read.sh 12345 # 读取单封邮件
scripts/mail-read.sh 12345 12346 12347 # 读取多封邮件(输出内容用分隔符区分)使用基于位置优化的查找(每封邮件约2秒)。多封邮件的输出内容用分隔,末尾附汇总信息。
========Errors
错误信息
| Error | Cause |
|---|---|
| Open Mail.app before running scripts |
| Invalid account — check mail-accounts.sh |
| Invalid/deleted ID — get fresh from mail-list.sh |
| Invalid name — check mail-mailboxes.sh |
| SQLite DB missing — check ~/Library/Mail/V{9,10,11}/MailData/ |
| 错误提示 | 原因 |
|---|---|
| 运行脚本前请打开Mail.app |
| 账户无效——请查看 |
| 消息ID无效/已删除——请从 |
| 邮箱名称无效——请查看 |
| SQLite数据库缺失——请检查路径 ~/Library/Mail/V{9,10,11}/MailData/ |
Technical Details
技术细节
Database:
~/Library/Mail/V{9,10,11}/MailData/Envelope IndexMessage lookup method (optimized):
- Query SQLite for account UUID, mailbox path, and approximate position
- AppleScript accesses the specific account directly (no iteration)
- Search starts at the approximate position (±5 messages buffer)
- Falls back to full mailbox search only if position hint fails
Safety:
- Fast search copies database to temp file before querying
- Safe to use even if Mail.app is running
- Delete/read/mark operations query live database but access is minimal
数据库路径:
~/Library/Mail/V{9,10,11}/MailData/Envelope Index消息查找方法(优化版):
- 查询SQLite获取账户UUID、邮箱路径和消息近似位置
- AppleScript直接访问指定账户(无需遍历)
- 从近似位置开始搜索(±5条消息缓冲)
- 仅当位置提示失效时,才回退到全邮箱搜索
安全性:
- 快速搜索会先将数据库复制到临时文件再查询
- 即使Mail.app运行时也可安全使用
- 删除/读取/标记操作会查询实时数据库,但访问量极小
Notes
注意事项
- Message IDs are internal, get fresh ones from list/search
- Confirm recipient before sending
- AppleScript search is slow but comprehensive; SQLite is fast for metadata
- Delete/mark operations support bulk actions (pass multiple IDs)
- Always refresh before listing if you need the absolute latest emails
¹ Known limitation: Mail.app adds a leading blank line to sent emails. This is an AppleScript/Mail.app behavior that cannot be bypassed.
- 消息ID为内部标识,请从列表/搜索结果中获取最新ID
- 发送邮件前请确认收件人
- AppleScript搜索速度慢但全面;SQLite搜索仅针对元数据,速度快
- 删除/标记操作支持批量处理(传入多个ID)
- 如果需要绝对最新的邮件,列出前务必先刷新
¹ 已知限制: Mail.app会在发送的邮件开头添加一个空白行。这是AppleScript/Mail.app的固有行为,无法规避。