Notmuch

出自 ArchWiki

Notmuch 是一个邮件索引器。本质上,它是 xapian 之上的一个非常薄的前端。很像 Sup,它专注于一件事:索引您的电子邮件。Notmuch 可以用作电子邮件阅读器,或者仅仅作为其他 MUA(如 mutt)的索引和搜索工具。

概述

Notmuch 是用 C 语言编写的,比 sup-mail 快一个数量级。Notmuch 可以在索引过程中终止,下次运行时它将从上次停止的地方继续。与 sup-mail 类似,它不提供永久删除不需要的电子邮件的方法(但是,请参阅 #永久删除邮件)。它不获取或发送邮件,也不存储您的电子邮件地址,您需要使用 OfflineIMAPmsmtpabook 等程序来完成这些任务。

安装 notmuch 软件包。它提供了 pythonvimemacs 绑定。

首次使用

安装完成后,您可以通过运行以下命令进入交互式设置

 notmuch setup

程序会提示您输入 maildir 的位置以及您的主要和辅助电子邮件地址。您也可以直接编辑默认在 $HOME/.notmuch-config 创建的配置文件。

后续邮件目录的重新索引通过以下命令完成

 notmuch new

前端

多种使用 notmuch 的方式,包括 CLI,或者使用 Unix $EDITORS 之一

Emacs

notmuch 的默认前端是 Emacs。它由开发 notmuch 的同一批人开发。

Vim

有一个 Vim 界面可用,并包含在 notmuch-vim 软件包中。要启动它,请输入

vim -c NotMuch

alot

alot 是 notmuch 的独立 CLI 界面,用 Python 编写。它以 alotalot-gitAUR 的形式提供。

Alot 使用 mailcap 处理不同类型的文件。这目前包括 HTML 邮件,这意味着您需要配置 ~/.mailcap 文件才能查看 HTML 邮件。至少,将此行放入您的 ~/.mailcap

 text/html; w3m -dump -o -document_charset=%{charset} %s; nametemplate=%s.html; copiousoutput

这使用 w3m,也可以使用其他基于文本的客户端,例如 linkslynx,尽管它们的参数可能有所不同。

当然,可以配置更多的文件处理程序。

bower

bower 是另一个 CLI 界面,这个是用 Mercury 编写的。它以 bower-mailAUR 的形式提供。

Neomutt

Neomutt - 另一个 mutt 分支,其中包含许多功能补丁,其中包括 Notmuch 集成补丁。安装 neomuttneomutt-gitAUR 软件包。

astroid

Astroid 是一个图形化的 MUA 和 notmuch 界面,使用 C++ 和 GTK 编写。可以使用 astroidastroid-gitAUR 安装。GUI 旨在非常快速,预览 HTML 和附件,可通过键盘导航。它具有广泛的可配置性,您可以使用您喜欢的编辑器,无论是嵌入式还是外部启动。查看 Tour 以了解如何使用 astroid 以及完整设置的描述,或查看 README 以获取更多信息。

与 mutt 集成

如果您使用 mutt 作为您的 MUA,那么 notmuch 是一个极好的补充工具,用于索引和搜索您的邮件。notmuch-mutt 软件包提供了一个脚本,用于将 notmuch 与 mutt 集成。

安装 notmuch-mutt 软件包并配置 notmuch 后,从 mutt 使用 notmuch 进行搜索之前剩下的唯一事情是添加快捷键,以从 mutt 调用 notmuch-mutt perl 脚本。在您的 .muttrc 中添加以下内容是在 notmuch contrib 源代码中推荐的做法

macro index <F8> \
"<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
<shell-escape>notmuch-mutt -r --prompt search<enter>\
<change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
<enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
"notmuch: search mail"

macro index <F9> \
"<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
<pipe-message>notmuch-mutt -r thread<enter>\
<change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
<enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
"notmuch: reconstruct thread"

macro index <F6> \
"<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
<pipe-message>notmuch-mutt tag -- -inbox<enter>\
<enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
"notmuch: remove message from inbox"

上面使用 F8 使用 notmuch 搜索您的收件箱,F9 从搜索结果创建线程,以及 F6 标记搜索结果。

与 NeoMutt 集成

如果您使用 neomutt,则不需要 notmuch-mutt 软件包。相反,创建一个 ~/.mailboxes 文件,其中包含一些基本(虚拟)邮箱。虚拟邮箱不是实际的文件夹,而是针对特定标签的 notmuch 查询结果

~/.mailboxes
virtual-mailboxes "inbox" "notmuch://?query=tag:inbox"
virtual-mailboxes "archive" "notmuch://?query=tag:archive"
virtual-mailboxes "sent" "notmuch://?query=tag:sent"
virtual-mailboxes "newsletters" "notmuch://?query=tag:newsletters"

接下来,通过启用虚拟假脱机文件并加载后者,使 mutt 意识到您的虚拟邮箱

~/.muttrc
set virtual_spoolfile=yes
set folder=notmuch-root-folder
source ~/.mailboxes

此时,mutt 仍然会将您的邮箱列为空,因为您的邮件尚未标记,因此 notmuch 查询为空。要填充您的虚拟邮箱,您需要首先标记您的 maildir 中的邮件。一个非常简单的方法是创建一个如下所示的 shell 脚本

~/.scripts/notmuch-hook.sh
#!/bin/sh
notmuch new
# retag all "new" messages "inbox" and "unread"
notmuch tag +inbox +unread -new -- tag:new
# tag all messages from "me" as sent and remove tags inbox and unread
notmuch tag -new -inbox +sent -- from:me@example.org or from:me@myself.com
# tag newsletters, but dont show them in inbox
notmuch tag +newsletters +unread -new -- from:newsletter@example.org or subject:'newsletter*'

使 shell 脚本 可执行 并运行它

 ~/.scripts/notmuch-hook.sh

为了使上面的示例工作,请确保 notmuch 将新邮件标记为“new”

~/.notmuch-config
[new]
tags=new

最后,您的 hook 脚本需要在新邮件到达时重新运行。如果您使用 offlineimap 将 IMAP 同步到本地 maildir,请创建一个 post sync hook

~/.offlineimaprc
[Account myaccount]
postsynchook = ~/.scripts/notmuch-hook.sh

一些有用的快捷键绑定

~/.muttrc
macro index A "<modify-labels>+archive -unread -inbox\\n" "Archive message"
macro index c "<change-vfolder>?" "Change to vfolder overview"
macro index \\\\ "<vfolder-from-query>" "Search mailbox"

永久删除邮件

一种选择是维护一个您希望从磁盘中删除的电子邮件标签,例如“killed”。然后,您可以将标签的搜索与 xargs 结合起来以永久删除它们

 notmuch search --output=files --format=text0 tag:killed | xargs -r0 rm
 notmuch new

通过将其放入 notmuch 的 pre-new hook 中,您可以确保在更新数据库之前删除文件。

参见