$npx -y skills add agentscope-ai/QwenPaw --skill himalaya-zh通过 IMAP/SMTP 管理邮件的命令行工具。使用 himalaya 可以在终端中列出、阅读、撰写、回复、转发、搜索和整理邮件。支持多账户和使用 MML(MIME Meta Language)撰写邮件。
| 1 | # Himalaya 邮件命令行工具 |
| 2 | |
| 3 | Himalaya 是一个命令行邮件客户端,可以通过 IMAP、SMTP、Notmuch 或 Sendmail 后端在终端中管理邮件。 |
| 4 | |
| 5 | ## 参考资料 |
| 6 | |
| 7 | - `references/configuration.md`(配置文件设置 + IMAP/SMTP 认证) |
| 8 | |
| 9 | ## 前置条件 |
| 10 | |
| 11 | 1. **Himalaya CLI** - `himalaya` 二进制文件必须已在 `PATH` 中。通过 `himalaya --version` 检查。 |
| 12 | - **推荐使用 v1.2.0 或更新版本。** 旧版本在某些 IMAP 服务器上可能会失败;v1.2.0+ 包含了相关修复。 |
| 13 | 2. 配置文件位于 `~/.config/himalaya/config.toml` |
| 14 | 3. 已配置 IMAP/SMTP 凭据(密码安全存储) |
| 15 | |
| 16 | ## 配置设置 |
| 17 | |
| 18 | 运行交互式向导来设置账户(将 `default` 替换为你想要的名称,例如 `gmail`、`work`): |
| 19 | |
| 20 | ```bash |
| 21 | himalaya account configure default |
| 22 | ``` |
| 23 | |
| 24 | 或手动创建 `~/.config/himalaya/config.toml`: |
| 25 | |
| 26 | ```toml |
| 27 | [accounts.personal] |
| 28 | email = "you@example.com" |
| 29 | display-name = "Your Name" |
| 30 | default = true |
| 31 | |
| 32 | backend.type = "imap" |
| 33 | backend.host = "imap.example.com" |
| 34 | backend.port = 993 |
| 35 | backend.encryption.type = "tls" |
| 36 | backend.login = "you@example.com" |
| 37 | backend.auth.type = "password" |
| 38 | backend.auth.cmd = "pass show email/imap" # 或使用 keyring |
| 39 | |
| 40 | message.send.backend.type = "smtp" |
| 41 | message.send.backend.host = "smtp.example.com" |
| 42 | message.send.backend.port = 587 |
| 43 | message.send.backend.encryption.type = "start-tls" |
| 44 | message.send.backend.login = "you@example.com" |
| 45 | message.send.backend.auth.type = "password" |
| 46 | message.send.backend.auth.cmd = "pass show email/smtp" |
| 47 | ``` |
| 48 | |
| 49 | 如果你使用的是 163 邮箱账户,请在配置文件中添加 `backend.extensions.id.send-after-auth = true` 以确保正常运作。 |
| 50 | |
| 51 | ## 常用操作 |
| 52 | |
| 53 | ### 列出文件夹 |
| 54 | |
| 55 | ```bash |
| 56 | himalaya folder list |
| 57 | ``` |
| 58 | |
| 59 | ### 列出邮件 |
| 60 | |
| 61 | 列出收件箱(默认)中的邮件: |
| 62 | |
| 63 | ```bash |
| 64 | himalaya envelope list |
| 65 | ``` |
| 66 | |
| 67 | 列出指定文件夹中的邮件: |
| 68 | |
| 69 | ```bash |
| 70 | himalaya envelope list --folder "Sent" |
| 71 | ``` |
| 72 | |
| 73 | 分页列出: |
| 74 | |
| 75 | ```bash |
| 76 | himalaya envelope list --page 1 --page-size 20 |
| 77 | ``` |
| 78 | |
| 79 | 如果遇到错误,请尝试: |
| 80 | |
| 81 | ```bash |
| 82 | himalaya envelope list -f INBOX -s 1 |
| 83 | ``` |
| 84 | |
| 85 | ### 搜索邮件 |
| 86 | |
| 87 | ```bash |
| 88 | himalaya envelope list from john@example.com subject meeting |
| 89 | ``` |
| 90 | |
| 91 | ### 阅读邮件 |
| 92 | |
| 93 | 通过 ID 阅读邮件(显示纯文本): |
| 94 | |
| 95 | ```bash |
| 96 | himalaya message read 42 |
| 97 | ``` |
| 98 | |
| 99 | 导出原始 MIME: |
| 100 | |
| 101 | ```bash |
| 102 | himalaya message export 42 --full |
| 103 | ``` |
| 104 | |
| 105 | ### 发送 / 撰写邮件 |
| 106 | |
| 107 | **推荐方式:** 使用 `template write | template send` 管道来发送简单邮件。 |
| 108 | |
| 109 | **发送一封简单邮件:** |
| 110 | |
| 111 | ```bash |
| 112 | export EDITOR=cat |
| 113 | himalaya template write \ |
| 114 | -H "To: recipient@example.com" \ |
| 115 | -H "Subject: Email Subject" \ |
| 116 | "Email body content" | himalaya template send |
| 117 | ``` |
| 118 | |
| 119 | **发送带多个头字段的邮件:** |
| 120 | |
| 121 | ```bash |
| 122 | export EDITOR=cat |
| 123 | himalaya template write \ |
| 124 | -H "To: recipient@example.com" \ |
| 125 | -H "Cc: cc@example.com" \ |
| 126 | -H "Subject: Email Subject" \ |
| 127 | "Email body content" | himalaya template send |
| 128 | ``` |
| 129 | |
| 130 | **发送带附件的邮件(使用 Python):** |
| 131 | |
| 132 | 对于带附件的邮件,请使用 Python 的 `smtplib` 和 `email.mime` 模块: |
| 133 | |
| 134 | ```python |
| 135 | import smtplib |
| 136 | from email.mime.multipart import MIMEMultipart |
| 137 | from email.mime.text import MIMEText |
| 138 | from email.mime.base import MIMEBase |
| 139 | from email import encoders |
| 140 | |
| 141 | msg = MIMEMultipart() |
| 142 | msg['From'] = 'sender@163.com' |
| 143 | msg['To'] = 'recipient@example.com' |
| 144 | msg['Subject'] = 'Email with attachment' |
| 145 | |
| 146 | msg.attach(MIMEText('Email body', 'plain')) |
| 147 | |
| 148 | # 添加附件 |
| 149 | with open('/path/to/file.pdf', 'rb') as f: |
| 150 | part = MIMEBase('application', 'octet-stream') |
| 151 | part.set_payload(f.read()) |
| 152 | encoders.encode_base64(part) |
| 153 | part.add_header('Content-Disposition', 'attachment; filename="file.pdf"') |
| 154 | msg.attach(part) |
| 155 | |
| 156 | server = smtplib.SMTP_SSL('smtp.163.com', 465) |
| 157 | server.login('sender@163.com', 'password') |
| 158 | server.send_message(msg) |
| 159 | server.quit() |
| 160 | ``` |
| 161 | |
| 162 | **注意:MML 附件限制:** 使用 MML 格式的 `template send` 命令在处理 multipart/附件时可能会失败,报错 "cannot parse MML message: empty body"。这是 himalaya v1.1.0 的已知问题。发送附件请使用 Python 方式。 |
| 163 | |
| 164 | **注意:避免在自动化中使用 `message write`:** `himalaya message write` 命令需要交互式 TUI 选择(Edit/Discard/Quit),在非交互式环境中会挂起。 |
| 165 | |
| 166 | **注意:`message send` 的限制:** 直接使用 `himalaya message send <raw_email>` 可能会因头字段解析问题而失败,报错 "cannot send message without a recipient"。请改用 `template send`。 |
| 167 | |
| 168 | **配置要求:** 确保在 config.toml 中设置了 `message.send.save-to-folder`,以避免 "Folder not exist" 错误: |
| 169 | |
| 170 | ```toml |
| 171 | [accounts.163] |
| 172 | # ... 其他配置 ... |
| 173 | message.send.save-to-folder = "Sent" |
| 174 | ``` |
| 175 | |
| 176 | 对于 163 邮箱账户,如果已发送文件夹不存在,请先创建: |
| 177 | |
| 178 | ```bash |
| 179 | himalaya folder create Sent |
| 180 | ``` |
| 181 | |
| 182 | ### 移动/复制邮件 |
| 183 | |
| 184 | 移动到文件夹: |
| 185 | |
| 186 | ```bash |
| 187 | himalaya message move 42 "Archive" |
| 188 | ``` |
| 189 | |
| 190 | 复制到文件夹: |
| 191 | |
| 192 | ```bash |
| 193 | himalaya message copy 42 "Important" |
| 194 | ``` |
| 195 | |
| 196 | ### 删除邮件 |
| 197 | |
| 198 | ```bash |
| 199 | himalaya message delete 42 |
| 200 | ``` |
| 201 | |
| 202 | ### 管理标记 |
| 203 | |
| 204 | 添加标记: |
| 205 | |
| 206 | ```bash |
| 207 | himalaya flag add 42 --flag seen |
| 208 | ``` |
| 209 | |
| 210 | 移除标记: |
| 211 | |
| 212 | ```bash |
| 213 | himalaya flag remove 42 --flag seen |
| 214 | ``` |
| 215 | |
| 216 | ## 多账户 |
| 217 | |
| 218 | 列出账户: |
| 219 | |
| 220 | ```bash |
| 221 | himalaya account list |
| 222 | ``` |
| 223 | |
| 224 | 使用指定账户: |
| 225 | |
| 226 | ```bash |
| 227 | himalaya --account work envelope list |
| 228 | ``` |
| 229 | |
| 230 | ## 附件 |
| 231 | |
| 232 | 保存邮件中的附件: |
| 233 | |
| 234 | ```bash |
| 235 | himalaya attachment download 42 |
| 236 | ``` |
| 237 | |
| 238 | 保存到指定目录: |
| 239 | |
| 240 | ```bash |
| 241 | himalaya attachment download 42 --dir ~/Downloads |
| 242 | ``` |
| 243 | |
| 244 | ## 输出格式 |
| 245 | |
| 246 | 大多数命令支持 `--output` 来获取结构化输出: |
| 247 | |
| 248 | ```bash |
| 249 | himalaya envelope list --output json |
| 250 | himalaya envelope list --output plain |
| 251 | ``` |
| 252 | |
| 253 | ## 调试 |
| 254 | |
| 255 | 启用调试日志: |
| 256 | |
| 257 | ```bash |
| 258 | RUST_LOG=debug himalaya envelope list |
| 259 | ``` |
| 260 | |
| 261 | 完整跟踪和回溯: |
| 262 | |
| 263 | ```bash |
| 264 | RUST_LOG=trace RUST_BACKTRACE=1 himalaya envel |