gog 是一个面向 Google Workspace 的命令行工具(CLI),可在终端中操作 Gmail、Calendar、Drive、Contacts、Sheets 和 Docs。它需要先完成一次 OAuth 授权设置,之后才能使用各项功能。
一、用途 gog 用于通过命令行访问和操作 Google Workspace 的常用服务,包括邮件、日历、云盘、联系人、表格和文档。它适合需要在终端、脚本或自动化流程中调用这些服务的场景。
二、授权设置(一次性) 使用前需配置 OAuth 凭据并添加账号:
gog auth credentials /path/to/client_secret.json
gog auth add you@gmail.com --services gmail,calendar,drive,contacts,docs,sheets
gog auth list
可通过设置环境变量 GOG_ACCOUNT=you@gmail.com 避免重复指定 --account。
三、主要能力
Gmail:
- 搜索线索(每行一个线索):gog gmail search 'newer_than:7d' --max 10
- 按单封邮件搜索(忽略线索归并):gog gmail messages search "in:inbox from:ryanair.com" --max 20 --account you@example.com
- 发送纯文本邮件:gog gmail send --to a@b.com --subject "Hi" --body "Hello"
- 通过文件发送多行邮件:gog gmail send --to a@b.com --subject "Hi" --body-file ./message.txt
- 通过标准输入发送:gog gmail send --to a@b.com --subject "Hi" --body-file -
- 发送 HTML 邮件:gog gmail send --to a@b.com --subject "Hi" --body-html "<p>Hello</p>"
- 创建草稿:gog gmail drafts create --to a@b.com --subject "Hi" --body-file ./message.txt
- 发送草稿:gog gmail drafts send <draftId>
- 回复邮件:gog gmail send --to a@b.com --subject "Re: Hi" --body "Reply" --reply-to-message-id <msgId>
Calendar:
- 列出事件:gog calendar events <calendarId> --from <iso> --to <iso>
- 创建事件:gog calendar create <calendarId> --summary "Title" --from <iso> --to <iso>
- 创建带颜色的事件:gog calendar create <calendarId> --summary "Title" --from <iso> --to <iso> --event-color 7
- 更新事件:gog calendar update <calendarId> <eventId> --summary "New Title" --event-color 4
- 查看颜色:gog calendar colors
事件颜色 ID(1-11)及其色值: 1: #a4bdfc;2: #7ae7bf;3: #dbadff;4: #ff887c;5: #fbd75b;6: #ffb878;7: #46d6db;8: #e1e1e1;9: #5484ed;10: #51b749;11: #dc2127。通过 --event-color <id> 为事件添加颜色。
Drive:
- 搜索:gog drive search "query" --max 10
Contacts:
- 列出联系人:gog contacts list --max 20
Sheets:
- 读取:gog sheets get <sheetId> "Tab!A1:D10" --json
- 更新:gog sheets update <sheetId> "Tab!A1:B2" --values-json '[["A","B"],["1","2"]]' --input USER_ENTERED
- 追加:gog sheets append <sheetId> "Tab!A:C" --values-json '[["x","y","z"]]' --insert INSERT_ROWS
- 清空:gog sheets clear <sheetId> "Tab!A2:Z"
- 元数据:gog sheets metadata <sheetId> --json
Docs:
- 导出:gog docs export <docId> --format txt --out /tmp/doc.txt
- 查看内容:gog docs cat <docId>
四、邮件格式说明 优先使用纯文本。多段落邮件使用 --body-file(或 --body-file - 从标准输入读取),草稿和回复同样适用该模式。--body 不会转义 \n,如需内联换行可使用 heredoc 或 $'Line 1\n\nLine 2'。仅在需要富文本格式时使用 --body-html,支持的标签包括 <p> 段落、<br> 换行、<strong> 加粗、<em> 斜体、<a href="url"> 链接、<ul>/<li> 列表。
示例(通过标准输入发送纯文本):
gog gmail send --to recipient@example.com
--subject "Meeting Follow-up"
--body-file - <<'EOF'
Hi Name,
Thanks for meeting today. Next steps:
- Item one
- Item two
Best regards, Your Name EOF
示例(HTML 列表):
gog gmail send --to recipient@example.com
--subject "Meeting Follow-up"
--body-html "<p>Hi Name,</p><p>Thanks for meeting today. Here are the next steps:</p><ul><li>Item one</li><li>Item two</li></ul><p>Best regards,<br>Your Name</p>"
五、其他说明
- 脚本场景建议使用 --json 搭配 --no-input。
- Sheets 的值可通过 --values-json(推荐)或内联行传递。
- Docs 支持导出、查看与复制;就地编辑需要 Docs API 客户端,gog 不提供该能力。
- 发送邮件或创建事件前应先确认。
- gog gmail search 每个线索返回一行;如需返回每封独立邮件,应使用 gog gmail messages search。