1---2title: "NeoMutt 的使用 (IMAP)"3date: 2022-02-20T14:59:04+08:004---56Mutt 是一个 TUI 的邮件客户端。78NeoMutt 在 Mutt 的基础上,新增了一些有用的[功能](https://neomutt.org/feature.html)。910Mutt/NeoMutt 相比于 KMail, ThunderBird 之类,更加的简洁明快,也有更高的可定制化空间。1112本文描述在 Linux 下,如何配置 NeoMutt 成为一个**可用**的邮件客户端。1314测试平台:15* Archlinux kernel ver 5.16.9-arch1-116* NeoMutt 2021102917* isync 1.4.4181920*此文不包含 smtp 发送邮件的配置,也许会在之后的文章中补充*2122<!--more-->2324## IMAP 邮件同步25Mutt/NeoMutt 可以直接连接 imap 服务器,但是在每次打开客户端时,都会需要连接同步 imap 服务器,笔者德国的电子邮局连接至少要 4-5 秒。2627将邮件定期与本地目录同步可以显著提升客户端打开速度,同步软件选用 isync 。2829如果不想同步到本地,可以直接参考 [Mutt - ArchWiki](https://wiki.archlinux.org/title/mutt) 配置帐号。3031### iSync 配置文件32isync 的默认配置文件为 `~/.mbsyncrc`,新建这个文件,并写入配置,单帐号的示例如下3334```35# 配置 IMAP 帐号36IMAPAccount lin # 自定义的帐号名称37Host imap.lin.moe # imap 服务器地址38User example@lin.moe # 帐号名39PassCmd "pass show mail/lin.moe/example" # 使用 pass 命令获取密码40SSLType IMAPS4142IMAPStore lin-remote43ACCOUNT lin4445MaildirStore lin-local46SubFolders Verbatim47# 确保 ~/Mail/lin 目录存在,或修改为其他目录48Path ~/Mail/lin/49Inbox ~/Mail/lin/Inbox5051Channel lin52Far :lin-remote:53Near :lin-local:54Patterns *55# Pattern 为你要同步的邮件文件夹名,如 Inbox Archive 等56Create Both57Expunge Both58SyncState *5960```61笔者使用 pass 命令管理密码,故在 PassCmd 中使用 `pass show`6263读者也可以使用 gpg 文件等方式存储密码,以下是简要说明6465密码明文写入到 ~/plain_pass 文件中后,使用下面的命令生成加密文件并删除明文文件(确保已有 gpg 密钥对)66```67gpg2 -e ~/plain_pass -o ~/.mailpass.gpg && rm -f ~/plain_pass68```69然后将 PassCmd 改成70```71PassCmd "gpg2 -q --for-your-eyes-only --no-tty -d ~/.mailpass.gpg"72```7374如果不想加密存储,也可以添加一行 `Pass *******`,不建议这样存储密码,请确保其他用户无读权限。757677更加详细的配置参考[官方文档](https://isync.sourceforge.io/mbsync.html)7879写好配置文件后,使用 `mbsync -a` 同步所有邮件。8081多帐号的配置较为简单,新添加多个类似的Account, Store 和 Channel 模块即可。82### 配置示例8384笔者的全量配置如下8586```87IMAPAccount lin88Host imap.lin.moe89User i@lin.moe90PassCmd "pass show mail/lin.moe/i"91SSLType IMAPS9293IMAPAccount lin-admin94Host imap.lin.moe95User admin@lin.moe96PassCmd "pass show mail/lin.moe/admin"97SSLType IMAPS9899IMAPStore lin-remote100ACCOUNT lin101102IMAPStore linadmin-remote103ACCOUNT lin-admin104105MaildirStore lin-local106SubFolders Verbatim107Path ~/Mail/lin/108Inbox ~/Mail/lin/Inbox109110MaildirStore linadmin-local111SubFolders Verbatim112Path ~/Mail/linAdmin/113Inbox ~/Mail/linAdmin/Inbox114115Channel lin116Far :lin-remote:117Near :lin-local:118Patterns *119Create Both120Expunge Both121SyncState *122123Channel linadmin124Far :linadmin-remote:125Near :linadmin-local:126Patterns *127Create Both128Expunge Both129SyncState *130```131132### 创建定期同步任务133方法1:参考 ArchWiki ,创建相关的 systemd 服务和定时器,每五分钟同步一次。134135```136mkdir -p ~/.config/systemd/user137cat >> ~/.config/systemd/user/mbsync.service << EOF138[Unit]139Description=Mailbox synchronization service140141[Service]142Type=oneshot143ExecStart=/usr/bin/mbsync -Va144145[Install]146WantedBy=default.target147EOF148149cat >> ~/.config/systemd/user/mbsync.timer << EOF150[Unit]151Description=Mailbox synchronization timer152153[Timer]154OnBootSec=2m155OnUnitActiveSec=5m156Unit=mbsync.service157158[Install]159WantedBy=timers.target160EOF161162systemctl --user --now enable mbsync.timer163```164165方法2:创建相关的 crontab 任务166167```168crontab -l | { cat; echo "*/5 * * * * mbsync -Va"; } | crontab -169```170171## 配置 neomutt172注:笔者配置文件存放在 ~/.config/neomutt 目录下,其他位置参考 neomutt 文档,并修改配置文件中的路径173174创建 ~/.config/neomutt/muttrc 文件,作为配置的入口175176示例如下177178```179# 邮件类型为本地文件夹180set mbox_type = Maildir181set folder = "~/Mail"182183# 邮件类型解析器配置184set mailcap_path = "~/.config/neomutt/mailcap"185186set quit=ask-yes187set wait_key = no188set timeout = 3189set mail_check = 0190set delete191set quit192set thorough_search193set mail_check_stats194195unset confirmappend196unset move197unset mark_old198unset beep_new199auto_view text/html200201# 快捷键配置202source "~/.config/neomutt/keybind"203204# 多帐号配置205206# 进入目录时,触发相关 source 命令,导入相关用户配置207folder-hook lin/* 'source ~/.config/neomutt/accounts/lin'208folder-hook linAdmin/* 'source ~/.config/neomutt/accounts/linAdmin'209210# 默认用户配置211source "~/.config/neomutt/accounts/lin"212```213214添加 html 类型邮件的在终端中的展示,可以使用 lynx 或 w3m215216这里使用 w3m,需要确保 w3m 已经安装。217218~/.config/neomutt/mailcap219220```221#text/html; lynx -dump %s; copiousoutput; nametemplate=%s.html222text/html; /usr/bin/w3m -cols 90 -dump -T text/html '%s'; copiousoutput223```224225226帐号配置示例如下227228~/.config/neomutt/accounts/lin229230```231set folder = "~/Mail/lin"232233set spoolfile = "+Inbox"234set postponed = "+Drafts"235set record = "+Sent"236set trash = "+Trash"237```238239按键绑定示例如下240241~/.config/neomutt/keybind242243```244# 'S' 同步邮件245macro index S "<shell-escape>mbsync -Va <enter>" "sync email"246247# 'c' 改变当前文件夹248macro index 'c' '<change-folder>?<change-dir><home>^K=<enter>'249250# vim 风格换行251bind pager j next-line252bind pager k previous-line253254# 移动邮件快捷键255bind index,pager g noop256macro 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"261262# 快速切换用户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```266267268完