blog-site

git clone git://git.lin.moe/blog-site.git

  1---
  2title: "NeoMutt 的使用 (IMAP)"
  3date: 2022-02-20T14:59:04+08:00
  4---
  5
  6Mutt 是一个 TUI 的邮件客户端。  
  7
  8NeoMutt 在 Mutt 的基础上,新增了一些有用的[功能](https://neomutt.org/feature.html)。  
  9
 10Mutt/NeoMutt 相比于 KMail, ThunderBird 之类,更加的简洁明快,也有更高的可定制化空间。  
 11
 12本文描述在 Linux 下,如何配置 NeoMutt 成为一个**可用**的邮件客户端。  
 13
 14测试平台:
 15* Archlinux kernel ver 5.16.9-arch1-1
 16* NeoMutt 20211029
 17* isync 1.4.4
 18
 19
 20*此文不包含 smtp 发送邮件的配置,也许会在之后的文章中补充*
 21
 22<!--more-->
 23
 24## IMAP 邮件同步
 25Mutt/NeoMutt 可以直接连接 imap 服务器,但是在每次打开客户端时,都会需要连接同步 imap 服务器,笔者德国的电子邮局连接至少要 4-5 秒。  
 26
 27将邮件定期与本地目录同步可以显著提升客户端打开速度,同步软件选用 isync 。  
 28
 29如果不想同步到本地,可以直接参考 [Mutt - ArchWiki](https://wiki.archlinux.org/title/mutt) 配置帐号。  
 30
 31### iSync 配置文件
 32isync 的默认配置文件为 `~/.mbsyncrc`,新建这个文件,并写入配置,单帐号的示例如下  
 33
 34```
 35# 配置 IMAP 帐号
 36IMAPAccount lin # 自定义的帐号名称
 37Host imap.lin.moe # imap 服务器地址
 38User example@lin.moe # 帐号名
 39PassCmd "pass show mail/lin.moe/example" # 使用 pass 命令获取密码
 40SSLType IMAPS
 41
 42IMAPStore lin-remote
 43ACCOUNT lin
 44
 45MaildirStore lin-local
 46SubFolders Verbatim
 47# 确保 ~/Mail/lin 目录存在,或修改为其他目录
 48Path ~/Mail/lin/ 
 49Inbox ~/Mail/lin/Inbox
 50
 51Channel lin
 52Far :lin-remote:
 53Near :lin-local: 
 54Patterns * 
 55# Pattern 为你要同步的邮件文件夹名,如 Inbox Archive 等
 56Create Both
 57Expunge Both
 58SyncState *
 59
 60```
 61笔者使用 pass 命令管理密码,故在 PassCmd 中使用 `pass show`   
 62
 63读者也可以使用 gpg 文件等方式存储密码,以下是简要说明   
 64
 65密码明文写入到 ~/plain_pass 文件中后,使用下面的命令生成加密文件并删除明文文件(确保已有 gpg 密钥对)
 66```
 67gpg2 -e ~/plain_pass -o ~/.mailpass.gpg && rm -f ~/plain_pass
 68```
 69然后将 PassCmd 改成
 70```
 71PassCmd "gpg2 -q --for-your-eyes-only --no-tty -d ~/.mailpass.gpg"
 72```  
 73  
 74如果不想加密存储,也可以添加一行 `Pass *******`,不建议这样存储密码,请确保其他用户无读权限。
 75
 76
 77更加详细的配置参考[官方文档](https://isync.sourceforge.io/mbsync.html)  
 78
 79写好配置文件后,使用 `mbsync -a` 同步所有邮件。
 80
 81多帐号的配置较为简单,新添加多个类似的Account, Store 和 Channel 模块即可。  
 82### 配置示例  
 83
 84笔者的全量配置如下
 85
 86```
 87IMAPAccount lin
 88Host imap.lin.moe
 89User i@lin.moe
 90PassCmd "pass show mail/lin.moe/i"
 91SSLType IMAPS
 92
 93IMAPAccount lin-admin
 94Host imap.lin.moe
 95User admin@lin.moe
 96PassCmd "pass show mail/lin.moe/admin"
 97SSLType IMAPS
 98
 99IMAPStore lin-remote
100ACCOUNT lin
101
102IMAPStore linadmin-remote
103ACCOUNT lin-admin
104
105MaildirStore lin-local
106SubFolders Verbatim
107Path ~/Mail/lin/
108Inbox ~/Mail/lin/Inbox
109
110MaildirStore linadmin-local
111SubFolders Verbatim
112Path ~/Mail/linAdmin/
113Inbox ~/Mail/linAdmin/Inbox
114
115Channel lin
116Far :lin-remote:
117Near :lin-local:
118Patterns *
119Create Both
120Expunge Both
121SyncState *
122
123Channel linadmin
124Far :linadmin-remote:
125Near :linadmin-local:
126Patterns *
127Create Both
128Expunge Both
129SyncState *
130```
131
132### 创建定期同步任务
133方法1:参考 ArchWiki ,创建相关的 systemd 服务和定时器,每五分钟同步一次。  
134
135```
136mkdir -p ~/.config/systemd/user
137cat >> ~/.config/systemd/user/mbsync.service << EOF
138[Unit]
139Description=Mailbox synchronization service
140
141[Service]
142Type=oneshot
143ExecStart=/usr/bin/mbsync -Va
144
145[Install]
146WantedBy=default.target
147EOF
148
149cat >> ~/.config/systemd/user/mbsync.timer << EOF
150[Unit]
151Description=Mailbox synchronization timer
152
153[Timer]
154OnBootSec=2m
155OnUnitActiveSec=5m
156Unit=mbsync.service
157
158[Install]
159WantedBy=timers.target
160EOF
161
162systemctl --user --now enable mbsync.timer
163```
164
165方法2:创建相关的 crontab 任务
166
167```
168crontab -l | { cat; echo "*/5 * * * * mbsync -Va"; } | crontab -
169```
170
171## 配置 neomutt
172注:笔者配置文件存放在 ~/.config/neomutt 目录下,其他位置参考 neomutt 文档,并修改配置文件中的路径  
173
174创建 ~/.config/neomutt/muttrc 文件,作为配置的入口  
175
176示例如下
177
178```
179# 邮件类型为本地文件夹
180set mbox_type = Maildir
181set folder = "~/Mail"
182
183# 邮件类型解析器配置
184set mailcap_path = "~/.config/neomutt/mailcap"
185
186set quit=ask-yes
187set wait_key = no
188set timeout = 3
189set mail_check = 0
190set delete
191set quit
192set thorough_search
193set mail_check_stats
194
195unset confirmappend
196unset move
197unset mark_old
198unset beep_new
199auto_view text/html
200
201# 快捷键配置
202source "~/.config/neomutt/keybind"
203
204# 多帐号配置
205
206# 进入目录时,触发相关 source 命令,导入相关用户配置
207folder-hook lin/* 'source ~/.config/neomutt/accounts/lin'
208folder-hook linAdmin/* 'source ~/.config/neomutt/accounts/linAdmin'
209
210# 默认用户配置
211source "~/.config/neomutt/accounts/lin"
212```
213
214添加 html 类型邮件的在终端中的展示,可以使用 lynx 或 w3m  
215
216这里使用 w3m,需要确保 w3m 已经安装。  
217
218~/.config/neomutt/mailcap
219
220```
221#text/html; lynx -dump %s; copiousoutput; nametemplate=%s.html
222text/html; /usr/bin/w3m -cols 90 -dump -T text/html '%s'; copiousoutput
223```
224
225
226帐号配置示例如下  
227
228~/.config/neomutt/accounts/lin
229
230```
231set folder = "~/Mail/lin"
232
233set spoolfile = "+Inbox"
234set postponed = "+Drafts"
235set record = "+Sent"
236set trash = "+Trash"
237```
238
239按键绑定示例如下  
240
241~/.config/neomutt/keybind
242
243```
244# 'S' 同步邮件
245macro index S "<shell-escape>mbsync -Va <enter>" "sync email"
246
247# 'c' 改变当前文件夹
248macro index 'c' '<change-folder>?<change-dir><home>^K=<enter>'
249
250# vim 风格换行
251bind pager j next-line
252bind pager k previous-line
253
254# 移动邮件快捷键
255bind index,pager g noop
256macro index,pager gi "<change-folder>=Inbox<enter>" "go to inbox"
257macro index,pager gs "<change-folder>=Sent<enter>" "go to sent"
258macro index,pager gd "<change-folder>=Drafts<enter>" "go to drafts"
259macro index,pager gt "<change-folder>=Trash<enter>" "go to trash"
260macro index,pager ga "<change-folder>=Archive<enter>" "go to archive"
261
262# 快速切换用户
263macro index <F2> '<sync-mailbox><enter-command>source ~/.config/neomutt/accounts/lin<enter><change-folder>!<enter>'
264macro index <F3> '<sync-mailbox><enter-command>source ~/.config/neomutt/accounts/linAdmin<enter><change-folder>!<enter>'
265```
266
267
268