技巧与窍门 / - 带 Mutt 去散步

作者:Kyle Rankin

Mutt 是我最喜欢的电子邮件客户端,也是我每天在工作和个人生活中都使用的客户端。关于 Mutt 最棒但最具挑战性的地方在于它令人难以置信的可配置性。当您使用一个程序时,您可能会想“我希望它可以做 X、Y 或 Z”,但在 Mutt 的情况下,您想要调整的大部分主要设置都可以更改。如果您像我一样使用 Mutt 多年,您会发现您的 .muttrc(Mutt 的主要配置文件)会经历几个阶段。当您开始使用 Mutt 时,您会花费大量时间试图弄清楚如何设置 Mutt 来读取您的邮件(通常是在别人的 .muttrc 的帮助下)。在您让 Mutt 工作后,下一个阶段涉及调整越来越复杂的选项,例如文件夹钩子。然而,最终,您的 .muttrc 会被微调到您喜欢的程度,并且您很少更改它。现在,我通常只在添加新邮箱时才更改我的 .muttrc。

我意识到,无论我多么认为 Mutt 很棒,如果其他人想第一次尝试它,学习曲线都有点令人生畏。在过去的专栏中,我讨论了 Mutt 的高级设置,但在本专栏中,我的目标是引导您完成 Mutt 用户在开始使用时最害怕的一件事:Mutt 配置。希望到本专栏结束时,您将拥有一个基本的、功能齐全的 Mutt 配置,您可以使用它来检查您的电子邮件。

Mutt 是一个 MUA

现在,Mutt 应该可以作为几乎所有主要发行版的软件包使用,所以我不会介绍如何安装它——只需使用您的软件包管理器即可。如果您习惯了常规的图形电子邮件客户端,那么 Mutt 有两个主要的不同之处。首先,它被设计为完全从键盘控制的终端运行。其次,Mutt 严格来说是一个邮件用户代理 (MUA),而不是邮件传输代理 (MTA)。大多数图形电子邮件客户端不仅可以作为 MUA 访问和读取您的电子邮件,而且他们也知道如何成为 MTA(他们可以通过 SMTP 直接与邮件服务器通信以发送电子邮件)。与这些客户端不同,Mutt 严格关注访问和读取您的邮件,并且它依赖于单独的 MTA。这意味着如果您的 Linux 系统尚未配置邮件服务器,您将需要设置一个基本的邮件服务器。如果您需要有关如何执行此操作的一些提示,请查看我在 2010 年 2 月刊中发表的“制作本地 Mutt 邮件服务器”专栏。

组织良好的 Mutt 配置

尽管您当然可以以任何您想要的方式设置您的 Mutt 配置(只要核心配置在 ~/.muttrc 中),但因为您是第一次这样做,所以您不妨设置一个配置文件系统,而不是一个巨大的 .muttrc。由于 Mutt 允许您从 .muttrc 中引用其他配置文件,因此许多 Mutt 用户将其选项组织到不同的文件中。我喜欢做的是将配置分隔为存储在 ~/.mutt 下的不同类别。我还将我的 .muttrc 文件存储在那里,并将 ~/.muttrc 文件符号链接到它。最后,我创建一个 ~/.muttrc.local 文件,我用它来存储任何我想保留在本地机器上的选项。这些选项包括是访问远程 IMAP 服务器还是本地 maildir,或其他此类本地设置。现在这看起来可能有很多工作,但重点是,一旦您将 Mutt 配置设置为您想要的方式,您只需将 ~/.mutt 目录 rsync 到您的其他机器,而无需擦除任何本地设置。

我意识到没有一个 Mutt 配置示例能够让每个人都满意,但这里有一些基本设置,我认为这些设置应该能让您顺利入门。我在我认为需要额外解释的选项中添加了注释,但有很多选项没有注释,因此如果您对某个选项的作用感到好奇,最好的资源是 Mutt 官方文档,网址为 mutt.org。我不时地浏览那里的文档,只是为了寻找一些新的(或对我来说是新的)我以前不知道我离不开的选项。

首先,让我们看一下我的主 ~/.mutt/.muttrc 文件。请记住,我实际上使用以下命令从此文件创建到 ~/.muttrc 的符号链接

ln -s ~/.mutt/.muttrc ~/.muttrc

此外,可能不用说,您还需要创建 ~/.mutt 目录。列表 1 显示了一个基本的入门级 ~/.mutt/.muttrc。

列表 1. 入门级 ~/.mutt/.muttrc

# Various client settings
set query_command="lbdbq %s"
set copy
set beep_new
set ascii_chars=yes
set reverse_name
set move=no
unset mark_old
set forward_quote
set include
set fast_reply="yes"
set indent_str="> "
# Cache email headers and store them in .muttheaders/
set header_cache="~/.muttheaders/"

# All the emails mutt should consider as being from me. 
# In this example, foo@example.org and bar@example.org are set.
set alternates=((foo|bar)@example.org) 

ignore *	# this means "ignore all headers by default"
# I do want to see these fields, though~
unignore	date from subject to cc

# local settings
source ~/.muttrc.local

# Color settings
source ~/.mutt/colors

# Where to store email aliases for people I email
set alias_file=~/.mutt/aliases
source ~/.mutt/aliases

# My list of mailboxes
source ~/.mutt/mailboxes

#mailing lists
subscribe talk@nblug.org
subscribe announce@nblug.org

# PGP/GPG settings
# If you don't have PGP set up, comment out these lines
set pgp_replyencrypt	# encrypt any replies to encrypted messages
set pgp_replysignencrypted	# automatically sign any messages 
                                # I encrypt
set pgp_show_unusable=no	# don't offer revoked keys in the 
                                # PGP key selection menu

# Folder hooks and other hook settings
source ~/.mutt/hooks

###################################
# Random and Weird Settings       #
###################################

# Editor Settings
# Use vim as the default editor with some special options for mutt
# such as spell check and 75 characters to a line
set editor="vim -c 'set nohlsearch noshowmatch modelines=0 tw=75 
 ↪et noai spell'"
# Show 7 lines of other email from a mailbox when reading 
# a specific email. Makes it easier to see where you are 
# in your mailbox when reading a message
set pager_index_lines=7		
# Keyboard Macros
macro index 	h "c?\t"	# show the "folder view" when 
                                # I hit 'h'

# extra weird settings
# this setting will highlight links and follow them 
# using w3m when I hit ctrl b
macro pager \cb \
<enter-command>'set pipe_decode'<enter>\
<pipe-entry>'w3m'<enter>\
<enter-command>'unset pipe_decode'<enter> \
'Follow links in w3m' 
auto_view text/html application/msword

#printer settings
set print_command="a2ps -g -Email -d -1 -M letter -R"

# abook settings
# abook is a cool command-line address book program 
# that works with mutt
set query_command="abook --mutt-query '%s'"
macro index \ca !abook\n
macro pager A |'abook --add-email'\n

正如您所看到的,这个 muttrc 文件相当复杂。除了我定义的大量选项列表外,我还包含了带有 source 选项的单独配置文件。例如,我将所有 Mutt 颜色配置都分离到了 ~/.mutt/colors 中。列表 2 显示了一个好的示例 ~/.mutt/colors 文件,您可以用来入门。

列表 2. 示例 ~/.mutt/colors

# color settings
color normal    white default
color attachment brightyellow default
color hdrdefault cyan default
color indicator brightwhite default
color markers   brightred default
color quoted    green default
color signature cyan default
color tilde     blue default
color tree      red default
color quoted1 green default

color index	brightyellow default ~N	# New
color index	yellow default ~O	# Old

#example of how to colorize based on FROM: 
#color index	magenta default '~f foo@example.com'

所有颜色选项都遵循相同的语法。首先是单词 color,然后是要着色的对象,最后是要使用的前景色和背景色。我使用 default 作为我的背景色,因此如果我有一个透明窗口,背景也是透明的。您会注意到索引(Mutt 窗口,列出邮箱中的所有消息)的颜色选项在末尾有一个额外的选项,可让您控制它应匹配哪些属性,然后才应用该颜色。例如,在这两个选项中

color index	brightyellow default ~N	# New
color index	yellow default ~O	# Old

~N 和 ~O 参数分别匹配任何新消息或旧消息。您可以使用 Mutt 广泛的匹配语言来匹配各种消息属性。在上面的文件中,我提供了一个注释示例,说明如何根据消息的 FROM: 标头对其进行着色。

本地 Mutt 设置

正如我之前提到的,我喜欢将任何可能在机器之间有所不同的设置分隔到 ~/.muttrc.local 文件中。如果您将所有电子邮件存储在本地 Maildir 文件夹中,以下是您可能想要保留在那里的设置示例

# local mbox settings
set mbox_type=Maildir
set folder=~/Maildir
set spoolfile=+INBOX
set record=+sent-mail
save-hook . "+saved-messages-`date +%Y`"
mailboxes "=INBOX"

以下是系统通过 IMAP 远程访问邮件的 .muttrc.local 示例

set folder=imaps://mail.example.net/INBOX
set imap_user=username
set imap_pass=password
set spoolfile=+
set record=+.sent-mail
save-hook . "=.saved-messages-`date +%Y`"

请注意,在这里我同时指定了 IMAP 用户名和 IMAP 密码。如果您想要额外的安全性,您将需要省略 imap_pass 选项,这样您的密码就不会以明文形式显示。如果未指定密码,Mutt 会在连接到该 IMAP 服务器时提示您。

Mutt 邮箱

在定义了您的主邮件文件夹设置后,您还需要定义除 INBOX 之外的任何其他邮箱。我将这些邮箱定义在 ~/.mutt/mailboxes 中,我应该注意这里的顺序很重要。您在配置文件中定义的任何邮箱都将由 Mutt 检查是否有新邮件。当您告诉 Mutt 更改为不同的邮箱时,它会自动用下一个有新邮件的邮箱名称填充邮箱名称。我经常使用此功能,尤其是在工作中,因为它使我可以确保首先处理所有具有新邮件的高优先级邮箱。这是一个示例 mailboxes 文件。请注意,= 符号告诉 Mutt 这些文件夹位于主文件夹之外

mailboxes "=linuxjournal"
mailboxes "=consulting"
mailboxes "=nblug"
mailboxes "=saved-messages"
mailboxes "=sent-mail"
钩子

最后一个值得一提的配置文件是 ~/.mutt/hooks,我在其中存储了所有文件夹钩子和其他设置。钩子是 Mutt 中的一个强大功能,它允许您根据您当前的文件夹、电子邮件的收件人或您回复电子邮件时的电子邮件内容动态更改您的 Mutt 设置。钩子语法可能有点复杂,所以我建议如果您想了解有关特定选项的更多信息,尤其是 index_format 和 folder_format 语法,请参考 Mutt.org 上的官方文档。列表 3 显示了我使用的一些示例钩子,用于更改某些文件夹中邮件的排序方式,调整在某些电子邮件消息上使用的签名,甚至在回复邮件时更改我的 TO 地址。

列表 3. 示例钩子

# The first options set defaults
unset sig_on_top
# default hook is 'set index_format="%4C %Z %{%b %d} 
# %-15.15L (%4l) %s"'
folder-hook . 'set index_format="%4C %Z %{%b %d} %-15.15L (%4l) %s"'
folder-hook . 'set sort=date'
folder-hook . 'my_hdr From: Kyle Rankin <foo@example.com>'
send-hook . unset signature

# Set special options when I'm in my nblug folder
folder-hook nblug 'set index_format="%4C %Z %{%b %d} 
 ↪%-15.15F (%4l) %s"'
folder-hook . 'set sort=date'
folder-hook nblug 'set sort=threads'
folder-hook nblug 'set signature="~/.mutt/.sig.nblug"

#default folder_format="%2C %t %N %F %sl %-8.8u %-8.8g %8s %d %f"
set folder_format="%2C %t %N %8s bytes - %d %f"

# these settings will pick a different signature file to use 
# depending on whether I'm sending email to nblug.org (one of my 
# mailing lists) or one of my consulting clients
send-hook '~t @nblug\.org$' 'set signature="~/.mutt/.sig.nblug"; 
 ↪my_hdr From: My Name <foo@example.com>'
send-hook '~t client@example\.com$' 
 ↪'set signature="~/.mutt/.sig.consulting"; my_hdr From: 
 ↪My Name <bar@example.com>'

# This is the actual hook I use to make sure emails to my 
# Linux Journal address have the proper FROM headers
reply-hook '~t lj@greenfly\.net' 'my_hdr 
 ↪From: Kyle Rankin <lj@greenfly.net>'

这就是您所拥有的。如果您的兴趣被 Mutt 激发,这些选项应该足以让您入门。我也知道这些设置不会吸引所有人。这就是 Mutt 的魅力所在——您可以更改选项,直到它们适合您为止。我仍然建议您在配置基本选项后,花一些时间阅读 Mutt.org 上的官方文档。那里有很多很棒的示例,并且还有许多比我在此处列出的更多的选项,这些选项可能会解决您遇到的特定配置问题。

Kyle Rankin 是旧金山湾区的系统架构师,也是许多本书的作者,包括Ubuntu Server 官方手册Knoppix HacksUbuntu Hacks。他目前是 North Bay Linux 用户组的主席。

加载 Disqus 评论