Mutt

From ArchWiki
(Redirected from Neomutt)

Mutt is a text-based mail client renowned for its powerful features. Though over two decades old, Mutt remains the mail client of choice for a great number of power users.

Mutt focuses primarily on being a Mail User Agent (MUA), and was originally written to view mail. Later implementations (added for retrieval, sending, and filtering mail) are simplistic compared to other mail applications and, as such, users may wish to use external applications to extend Mutt's capabilities.

Nevertheless, the Arch Linux mutt package is compiled with IMAP, POP3 and SMTP support, removing the necessity for external applications.

This article covers using both native IMAP sending and retrieval, and a setup depending on OfflineIMAP or getmail (POP3) to retrieve mail, procmail to filter it in the case of POP3, and msmtp to send it.

Installation

Install the mutt package. A popular fork with more features is neomutt.

Optionally install external helper applications for an IMAP setup, such as isync or OfflineIMAP.

Or (if using POP3) getmailAUR, mpop, fetchmail or fdm and procmailAUR.

Note: About authentication setups
  • If you just need the methods LOGIN and PLAIN, these are satisfied with the dependency libsasl
  • If you want to (or have to) use CRAM-MD5, GSSAPI or DIGEST-MD5, install the package cyrus-sasl-gssapi
  • If you are using Gmail as your SMTP server, you may need to install the package cyrus-sasl

Configuration

This section covers #IMAP, #POP3, #Maildir and #SMTP configuration.

Mutt will, by default, search six locations for its configuration file; ~/.muttrc, ~/.mutt/muttrc, and $XDG_CONFIG_HOME/mutt/muttrc, first with -MUTT_VERSION appended, then without. Any of these locations will work. In case you decide to put the initialization file somewhere else, use $ mutt -F /path/to/.muttrc. You should also know some prerequisite for Mutt configuration. Its syntax is very close to the Bourne Shell. For example, you can get the content of another configuration file

source /path/to/other/config/file

You can use variables and assign the result of shell commands to them.

set editor=`echo \$EDITOR`

Here the $ gets escaped so that it does not get substituted by Mutt before being passed to the shell. Also note the use of the backquotes, as bash syntax $(...) does not work. Mutt has a lot of predefined variables, but you can also set your own. User variables must begin with "my"!

set my_name = "John Doe"

IMAP

Native IMAP support

The mutt package is compiled with IMAP support. At the very least you need to have four lines in your muttrc file to be able to access your mail.

imap_user
set imap_user=USERNAME
Tip: Continuing with the previous example, remember that Gmail requires your full email address (this is not standard)
set imap_user=your.username@gmail.com
imap_pass

If unset, the password will be prompted for.

set imap_pass=SECRET
Tip: If you have enabled two-factor authentication in Gmail and you have added an application specific password for Mutt, you will want to use that password here rather than your regular Gmail password.
folder

Instead of a local directory which contains all your mail (and directories), use your server (and the highest folder in the hierarchy, if needed).

set folder=imap[s]://imap.server.domain[:port]/[folder/]

You do not have to use a folder, but it might be convenient if you have all your other folders inside your INBOX, for example. Whatever you set here as your folder can be accessed later in Mutt with just an equal sign (=) or a plus sign (+). Example

set folder=imaps://imap.gmail.com/

It should be noted that for several accounts, it is best practice to use different folders -- e.g. for account-hook. If you have several Gmail accounts, use

set folder=imaps://username@imap.gmail.com/

instead, where your account is username@gmail.com. This way it will be possible to distinguish the different folders. Otherwise it would lead to authentication errors.

spoolfile

The spoolfile is the folder where your (unfiltered) e-mail arrives. Most e-mail services conventionally name it INBOX. You can now use '=' or '+' as a substitution for the full folder path that was configured above. For example

set spoolfile=+INBOX
mailboxes

Any IMAP folders that should be checked regularly for new mail should be listed here

mailboxes =INBOX =family
mailboxes imaps://imap.gmail.com/INBOX imaps://imap.gmail.com/family

Alternatively, check for all subscribed IMAP folders (as if all were added with a mailboxes line)

set imap_check_subscribed

These two versions are equivalent if you want to subscribe to all folders. So the second method is much more convenient, but the first one gives you more flexibility. Also, newer Mutt versions are configured by default to include a macro bound to the 'y' key which will allow you to change to any of the folders listed under mailboxes.

If you do not set this variable, the spoolfile will be used by default. This variable is also important for the #Sidebar.

Summary

Using these options, you will be able to start Mutt, enter your IMAP password, and start reading your mail. Here is a muttrc snippet (for Gmail) with some other lines you might consider adding for better IMAP support.

set folder      = imaps://imap.gmail.com/
set imap_user   = your.username@gmail.com
set imap_pass   = your-imap-password
set spoolfile   = +INBOX
mailboxes       = +INBOX

# Store message headers locally to speed things up.
# If hcache is a folder, Mutt will create sub cache folders for each account which may speeds things up even more.
set header_cache = ~/.cache/mutt

# Store messages locally to speed things up, like searching message bodies.
# Can be the same folder as header_cache.
# This will cost important disk usage according to your e-mail amount.
set message_cachedir = "~/.cache/mutt"

# Specify where to save and/or look for postponed messages.
set postponed = +[Gmail]/Drafts

# Allow Mutt to open a new IMAP connection automatically.
unset imap_passive

# Keep the IMAP connection alive by polling intermittently (time in seconds).
set imap_keepalive = 300

# How often to check for new mail (time in seconds).
set mail_check = 120

External IMAP support

While IMAP support is built into Mutt, it does not download mail for offline use. It is possible to use an external application such as OfflineIMAP or isync to download your emails to a local folder which can then be processed by Mutt.

Consider using applications such as spamassassin or imapfilterAUR to sort mail.

POP3

The mutt package is compiled with POP3 support, which is configured via the pop_* variables as described in muttrc(5).

Alternatively, it is possible to use external programs to fetch mail using POP3. One popular option is to use getmail for retrieving and procmail for filtering the mail.

Maildir

Maildir is a generic and standardized format. Almost every MUA is able to handle Maildirs and Mutt's support is excellent. There are just a few simple things that you need to do to get Mutt to use them. Open your muttrc and add the following lines

set mbox_type=Maildir
set folder=~/mail
set spoolfile=+/
set header_cache=~/.cache/mutt

This is a minimal Configuration that enables you to access your Maildir and checks for new local Mails in INBOX. This configuration also caches the headers of the eMails to speed up directory-listings. It might not be enabled in your build (but it sure is in the Arch-Package). Note that this does not affect OfflineIMAP in any way. It always syncs all of the directories on a Server. spoolfile tells Mutt which local directories to poll for new Mail. You might want to add more Spoolfiles (for example the Directories of Mailing-Lists) and maybe other things. But this is subject to the Mutt manual and beyond the scope of this document.

SMTP

Whether you use POP or IMAP to receive mail you will probably still send mail using SMTP.

Folders

There is basically only one important folder here: the one where all your sent e-mails will be saved.

set record = +Sent

Gmail automatically saves sent e-mail to +[Gmail]/Sent, so we do not want duplicates.

unset record

Native SMTP support

The mutt package is compiled with SMTP support.

Here are the common SMTP settings

set realname = 'Your Real Name'
set from = your-email-address

set smtp_pass = $imap_pass
set ssl_force_tls = yes

If your SMTP server is STARTTLS-based, add the following

set smtp_url=smtp://$imap_user@smtp.domain.tld:587
set ssl_starttls = yes

If your SMTP server is SMTPS-based, add the following instead

set smtp_url=smtps://$my_user@smtp.domain.tld

If your SMTP credentials are not the same as your IMAP credentials, then you must use the correct credentials instead of $imap_user and $imap_pass.

If you get an error like SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol, then your server probably uses STARTTLS instead of SMTPS.

If your server uses the LOGIN authentication method you might need to specify this explicitly, despite the manual's claim that all methods are tried by default

set smtp_authenticators = "login"

See muttrc(5) for more information.

External SMTP support

An external SMTP agent such as msmtp, sSMTP or opensmtpd can also be used.

The sendmail variable in your muttrc determines the program and arguments used to deliver mail in mutt. Any additional arguments must be interpreted by the program as recipient addresses.

For example, if using msmtp

muttrc
set realname='Disgruntled Kangaroo'

set sendmail="/usr/bin/msmtp"

set edit_headers=yes
set folder=~/mail
set mbox=+mbox
set spoolfile=+inbox
set record=+sent
set postponed=+drafts
set mbox_type=Maildir

mailboxes +inbox +lovey-dovey +happy-kangaroos

Sending mails from Mutt

Now, startup mutt

You should see all the mail in ~/mail/inbox. Press m to compose mail; it will use the editor defined by your EDITOR environment variable. If this variable is not set, you can fix it before starting Mutt

$ export EDITOR=your-favorite-editor
$ mutt

You should store the EDITOR value into your shell resource configuration file (such as bashrc). You can also set the editor from Mutt's configuration file

.muttrc
set editor=your-favorite-editor

For testing purposes, address the letter to yourself. After you have written the letter, save and exit the editor. You will return to Mutt, which will now show information about your e-mail. Press y to send it.

Warning: If at this point you press q by accident, Mutt will ask you Postpone this message? ([yes]/no). This is really asking whether you want to save the message you just wrote. If you press "n" (perhaps because you want to edit the message again) the message will be permanently deleted. When using Mutt, always remember that "Postpone this message?" really means "Save this message?".

Multiple accounts

Now you should have a working configuration for one account at least. You might wonder how to use several accounts, since we put everything into a single file.

Well all you need is to write account-specific parameters to their respective files and source them. All the IMAP/POP3/SMTP configuration for each account should go to its respective folder.

Warning: When one account is setting a variable that is not specified for other accounts, you must unset it for them, otherwise configuration will overlap and you will most certainly experience unexpected behaviour.

Mutt can handle this thanks to one of its most powerful features: hooks. Basically a hook is a command that gets executed before a specific action. There are several hooks available. For multiple accounts, you must use account-hooks and folder-hooks.

  • Folder-hooks will run a command before switching folders. This is mostly useful to set the appropriate SMTP parameters when you are in a specific folder. For instance when you are in your work mailbox and you send a e-mail, it will automatically use your work account as sender.
  • Account-hooks will run a command every time Mutt calls a function related to an account, like IMAP syncing. It does not require you to switch to any folder.

Hooks take two parameters

account-hook [!]regex command
folder-hook [!]regex command

The regex is the folder to be matched (or not if preceded by the !). The command tells what to do.

Let us give a full example

.muttrc
## General options
set header_cache = "~/.cache/mutt"
set imap_check_subscribed
set imap_keepalive = 300
unset imap_passive
set mail_check = 60
set mbox_type=Maildir

## ACCOUNT1
source "~/.mutt/work"
# Here we use the $folder variable that has just been set in the sourced file.
# We must set it right now otherwise the 'folder' variable will change in the next sourced file.
folder-hook $folder 'source ~/.mutt/work'

## ACCOUNT2
source "~/.mutt/personal"
folder-hook *user@gmail.com/ 'source ~/.mutt/personal'
folder-hook *user@gmail.com/Family 'set realname="Bob"'
.mutt/work
## Receive options.
set imap_user=user@gmail.com
set imap_pass=****
set folder = imaps://user@imap.gmail.com/
set spoolfile = +INBOX
set postponed = +Drafts
set record = +Sent

## Send options.
set smtp_url=smtps://user:****@smtp.gmail.com
set realname='User X'
set from=user@gmail.com
set hostname="gmail.com"
set signature="John Doe"
# Connection options
set ssl_force_tls = yes
unset ssl_starttls

## Hook -- IMPORTANT!
account-hook $folder "set imap_user=user@gmail.com imap_pass=****"

Finally .mutt/personal should be similar to .mutt/work.

Now all your accounts are set, start Mutt. To switch from one account to another, just change the folder (c key). Alternatively you can use the sidebar.

To change folder for different mailboxes you have to type the complete address -- for IMAP/POP3 folders, this may be quite inconvenient -- let us bind some key to it.

## Shortcuts
macro index,pager <f2> '<sync-mailbox><enter-command>source ~/.mutt/personal<enter><change-folder>!<enter>'
macro index,pager <f3> '<sync-mailbox><enter-command>source ~/.mutt/work<enter><change-folder>!<enter>'

With the above shortcuts (or with the sidebar) you will find that changing folders (with c by default) is not contextual, i.e. it will not list the folders of the current mailbox, but of the one used the last time you changed folders. To make the behaviour more contextual, the trick is to press = or + for current mailbox. You can automate this with the following macro

macro index 'c' '<change-folder>?<change-dir><home>^K=<enter>'

Alternative way

Another method to use mutt with multiple accounts is to run multiple mutt instances. You can have common settings in .muttrc and account specific settings in their respective files. Now you can source .muttrc from account specific config file. Advantage of this method is folder switching is not required to access different accounts, which improves workflow and saves time. For example,

.muttrc
## General options
set header_cache = "~/.cache/mutt"
set imap_check_subscribed
set imap_keepalive = 300
unset imap_passive
set mail_check = 60
set mbox_type=Maildir
.mutt/work
source .muttrc

## Receive options.
set imap_user=user@gmail.com
set imap_pass=****
set folder = imaps://user@imap.gmail.com/
set spoolfile = +INBOX
set postponed = +Drafts
set record = +Sent

## Send options.
set smtp_url=smtps://user:****@smtp.gmail.com
set realname='User X'
set from=user@gmail.com
set hostname="gmail.com"
set signature="John Doe"
# Connection options
set ssl_force_tls = yes
unset ssl_starttls

Finally .mutt/personal should be similar to .mutt/work. Now you can run mutt with

mutt -F .mutt/work
mutt -F .mutt/personal

or create alias mutt_work in your .bashrc

alias mutt_work="mutt -F .mutt/work"
alias mutt_personal="mutt -F .mutt/personal"

or write a script to start all mutt instances in one go,

#!/bin/sh
lxterminal -e mutt -F .mutt/work
lxterminal -e mutt -F .mutt/personal

Passwords management

Keep in mind that writing your password in .muttrc is a security risk. One solution is to always enter the password manually, but this becomes cumbersome.

GPG

An alternative solution is to encrypt your password with GnuPG in an encrypted file. Setup your own keypair if you have not done so already. Create a file in a tmpfs with the following contents

set my_pass = "password"
Note: Remember that user defined variables must start with my_.

Then encrypt this file, setting yourself as the recipient and move it into an accessible location. In this example the encrypted file resides at $HOME/.my-pwds.gpg.

In your mutt configuration file add the following before any account

source "gpg -dq $HOME/.my-pwds.gpg |"
Note: At the end of the line above, there is no space between the pipe and the double quote.

This decrypts the file quietly and sets the variable my_pass in this example. This can be used in any variable after it has been sourced. For example

set imap_pass=$my_pass

If you use external tools like OfflineIMAP and msmtp, you need to set up an agent (e.g. gpg-agent, see GnuPG#gpg-agent) to keep the passphrase into cache and thus avoiding those tools always prompting for it.

Pass

You can also use pass to encrypt your passwords easily. Just add the passwords to the given passwords as follows

pass add user@domain.tld

After that, just add the command pass show user@domain.tld in your muttrc, for instance

set imap_pass="`pass show user@domain.tld`"

Note the use of both backquotes and regular quotes to prevent the output of the backquotes from being parsed[1].

Security concern

If enter-command is available from the UI, it is possible to see the password unencrypted, which may be undesired if anybody else than you has access to your session while Mutt is running. You may want to disable it for this reason. As a consequence, every command that the user intends to use must be bound to a key in advance, otherwise it will never be accessible.

.muttrc
 bind generic,alias,attach,browser,editor,index,compose,pager,pgp,postpone ':' noop

Tips and tricks

Guides to get you started with using and customizing Mutt

If you have any Mutt specific questions, feel free to ask in the Arch IRC channels.

Key bindings

The default key bindings are quite far from the more common Emacs-like or Vi-like bindings. You can customize them to your preference. Mutt has a different set of bindings for the pager, the index, the attachment view, etc. Thus you need to specify which map you want to modify when you bind a key. You can review the list of commands and key bindings from Mutt's help page (default key: ?). Example of Vi-like bindings

muttrc
## h to exit in every menu, l to go deeper, j and k for up and down, g and G for top and bottom
bind pager,browser			h			exit
bind attach,alias			h			exit
bind pager				l			view-attachments
bind pager 				k 			previous-line
bind pager 				j 			next-line
bind pager				g			top
bind pager				G			bottom
bind index				j			next-entry
bind index				k			previous-entry
bind attach,index 			g 			first-entry
bind attach,index 			G 			last-entry
bind index				l			display-message
bind attach				l			view-attach
bind browser,alias			l			select-entry
bind index				/			search
## default binding for limit is l
bind index				?			limit
## default binding for help is ?
bind index,pager,attach			<F1>			help
## default binding for header view (toggle-weed) is h
bind pager				H			display-toggle-weed
bind attach,index,pager 		\CD 			next-page
bind attach,index,pager 		\CU 			previous-page

Composition

Encrypt and sign mail (GnuPG)

To start encrypting mail in mutt using GnuPG copy /usr/share/doc/mutt/samples/gpg.rc to your mutt configuration folder (e.g. to ~/.mutt/gpg.rc). Then append the following to your mutt configuration file (e.g. ~/.mutt/muttrc)

source ~/.mutt/gpg.rc

Most encryption options are then available by pressing p in the compose view.

See the pgp_* and crypt_* options in muttrc(5).

E-mail character encoding

When using Mutt there are two levels where the character sets that must be specified

  • The text editor used to write the e-mail must save it in the desired encoding.
  • Mutt will then check the e-mail and determine which encoding is the more appropriate according to the priority you specified in the send_charset variable. Default: "us-ascii:iso-8859-1:utf-8".

So if you write an e-mail with characters allowed in ISO-8859-1 (like 'résumé'), but without characters specific to Unicode, then Mutt will set the encoding to ISO-8859-1.

To avoid this behaviour, set the variable in your muttrc

set send_charset="us-ascii:utf-8"

or even

set send_charset="utf-8"

The first compatible charset starting from the left will be used. Since UTF-8 is a superset of US-ASCII it does not harm to leave it in front of UTF-8, it may ensure old MUA will not get confused when seeing the charset in the e-mail header.

Custom mail headers

One of the greatest thing in Mutt is that you can have full control over your mail header.

First, make your headers editable when you write e-mails

set edit_headers=yes

Mutt also features a special function my_hdr for customizing your header. Yes, it is named just like a variable, but in fact it is a function.

You can clear it completely, which you should do when switching accounts with different headers, otherwise they will overlap

unmy_hdr *

Other variables have also an impact on the headers, so it is wise to clear them before using my_hdr

unset use_from
unset use_domain
unset user_agent

Now, you can add any field you want -- even non-standard one -- to your header using the following syntax

my_hdr <FIELD>: <VALUE>

Note that <VALUE> can be the result of a command.

Example

## Extra info.
my_hdr X-Info: Keep It Simple, Stupid.

## OS Info.
my_hdr X-Operating-System: `uname -s`, kernel `uname -r`

## This header only appears to MS Outlook users
my_hdr X-Message-Flag: WARNING!! Outlook sucks

## Custom Mail-User-Agent ID.
my_hdr User-Agent: Every email client sucks, this one just sucks less.

## Receive copy of sent mail in your inbox
my_hdr Cc: $imap_user

Signature block

Create a .signature in your home directory. Your signature will be appended at the end of your email. Alternatively you can specify a file in your Mutt configuration

set signature="path/to/sig/file"
Random signature

You can use fortune (package fortune-mod) to add a random signature to Mutt.

Create a fortune file and then add the following line to your .muttrc

set signature="fortune pathtofortunefile|"

Note the pipe at the end. It tells Mutt that the specified string is not a file, but a command.

Compose and send from command line

Man pages will show all available commands and how to use them, but here are a couple of examples. You could use Mutt to send alerts, logs or some other system information, triggered by login through .bash_profile, or as a regular cron job.

Send a message

mutt -s "Subject" somejoeorjane@someserver.com < /var/log/somelog

Send a message with attachment

mutt -s "Subject" somejoeorjane@someserver.com -a somefile < /tmp/sometext.txt

Compose HTML e-mails

由于 Mutt 没有 WYSIWYIG 客户端,HTML 非常简单直接,而且您可以比所有 WYSIWYIG 邮件客户端做得更多,因为您可以直接编辑源代码。只需使用 HTML 语法编写邮件即可。例如

This is normal text<br>
<b>This is bold text</b>

现在,在发送邮件之前,使用 edit-type 命令(默认快捷键 Ctrl+t),并将 text/plain 替换为 text/html

注意: 许多人认为 HTML 电子邮件无用、累赘且容易出现阅读问题。Mutt 可以使用像 w3m 或 lynx 这样的文本浏览器读取 HTML 邮件,但它显然没有比纯文本电子邮件有任何优势。您应尽可能避免编写 HTML 电子邮件。

撰写邮件时显示另一封邮件

关于 Mutt 的一个常见抱怨是,当撰写新邮件(或回复)时,您无法打开另一封邮件(例如,为了与另一位联系人核对),而不关闭当前邮件(推迟发送)。以下描述了一种解决方案

首先,像往常一样启动 Mutt。然后,启动另一个终端窗口。现在使用以下命令启动一个新的 Mutt:

mutt -R

这将以只读模式启动 Mutt,您可以方便地浏览其他电子邮件。强烈建议始终以只读模式启动第二个 Mutt,否则很容易出现冲突。

注意: 当更改文件夹(使用 cy)时,只读模式不会保留。而必须使用 Esc c
提示: 此解决方案需要输入一些内容,因此适合将以下命令绑定到键盘快捷键
$TERMINAL -e mutt -R
其中 $TERMINAL 是您的终端。

打印

您可以安装 muttprintAUR 以获得更精美的打印质量。在您的 muttrc 文件中,插入

set print_command="/usr/bin/muttprint %s -p {PrinterName}"

查看内容

在 Web 浏览器中查看 URL

这可以使用 urlscanurlviewAURextract_urlAUR 完成。要使用 urlscan,请安装 urlscan 并在 mutt 配置文件中插入以下内容。

 macro index,pager \cb "<pipe-message> urlscan<Enter>" "call urlscan to extract URLs out of a message"
 macro attach,compose \cb "<pipe-entry> urlscan<Enter>" "call urlscan to extract URLs out of a message"

按下 ctrl+b 将列出电子邮件中的所有 URL。BROWSER 环境变量可用于选择默认 Web 浏览器。

默认按键绑定可以使用 F1 键列出。除非需要额外的按键绑定或颜色方案,否则无需为 urlscan 创建配置文件。如果需要,可以使用以下命令在 ˜/.config/urlscan/config.json 创建默认配置文件。

 urlscan -g

要使用 urlview 代替 urlscan,请安装 urlviewAUR 软件包,并在 mutt 配置文件中添加以下行。

 macro index,pager \cb "<pipe-message> urlview<Enter>" "call urlview to extract URLs out of a message"
 macro attach,compose \cb "<pipe-entry> urlview<Enter>" "call urlview to extract URLs out of a message"

在 $HOME 中创建一个 .urlview 文件并插入以下内容

REGEXP (((http|https|ftp|gopher)|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]
COMMAND <your-browser> %s

在使用 mutt 时按下 ctrl+b 将列出电子邮件中的所有 URL。使用箭头键上下导航,并在所需的 URL 上按 Enter 键。您的浏览器将启动并转到选定的站点。

某些浏览器需要额外的参数才能正常工作。例如,Luakit 会在 Mutt 退出时关闭。您需要使用 -n 参数将其 fork 到后台

COMMAND luakit -n %s 2>/dev/null

2>/dev/null 是为了使其静默,即防止打印您不希望看到的无用消息。

注意: urlview 有一些缺陷(例如,无法处理某些电子邮件编码),并且功能相当简陋(例如,它不提供其找到的链接的上下文)。extract_urlAUR 可以处理所有电子邮件编码并提供链接上下文。urlscan 可以提供链接上下文,但无法处理所有电子邮件编码。两者都可以作为 urlview 的直接替代品,尽管 extract_url 具有从额外配置更改中受益的功能。

查看 HTML

可以将 HTML 正文传递给外部 HTML 程序,然后转储它,保持电子邮件查看的统一和不突兀。此处描述了三个程序:lynxw3melinks(确保已安装选定的软件包)。

如果 ~/.mutt/mailcap 不存在,您需要创建它并将以下内容保存到其中。

text/html; lynx -assume_charset=%{charset} -display_charset=utf-8 -collapse_br_tags -dump %s; nametemplate=%s.html; copiousoutput

或者,如果是 w3m,

text/html; w3m -I %{charset} -T text/html; copiousoutput;

或者,如果是 elinks,

text/html; elinks -dump; copiousoutput;

或者,如果是 elinks(带颜色支持),

text/html; elinks -dump -dump-color-mode 1; copiousoutput;

编辑 ~/.muttrc 并添加以下内容,

set mailcap_path   = ~/.mutt/mailcap

要自动在 lynx、w3m 或 elinks 中打开 HTML 消息,请将此附加行添加到 muttrc

auto_view text/html

这样做的好处是,您不必将 HTML 正文视为源代码或由单独的程序打开,在这种情况下是 lynx,您可以直接看到格式化的内容,并且电子邮件中的任何 URL 链接都可以通过 Ctrl+b 显示,前提是您已安装并配置了 urlscan,如上所述。

如果您收到许多具有多个或备用编码的电子邮件,Mutt 可能会默认将每封电子邮件都视为 HTML。为了避免这种情况,请将以下变量添加到您的 ~/.muttrc,以便 Mutt 在文本版本可用时默认使用文本,并且仅在电子邮件中没有文本版本时才使用 w3m/lynx

alternative_order text/plain text/html

某些 HTML 邮件可能无法在基于文本的 Web 浏览器中正确显示。作为一种备用解决方案,您可以绑定一个键,以便在这种情况下打开图形浏览器。以下宏将在 Web 浏览器中打开从附件视图中选择的 HTML 邮件,该 Web 浏览器在环境中定义。(随意调整 ~/.cache/mutt/ 文件夹)。

macro attach 'V' "<pipe-entry>iconv -c --to-code=UTF8 > ~/.cache/mutt/mail.html<enter><shell-escape>$BROWSER ~/.cache/mutt/mail.html<enter>"

如果 $BROWSERfirefox,则在 about:config 中将 intl.charset.fallback.utf8_for_file 设置为 true,如 [2] 中所述,以确保 HTML 页面中的非 ASCII 字符正确显示。

要将 HTML 电子邮件转换为经过清理的 markdown 并使用 mutt 的内部分页器查看结果输出,请安装 python-html2text 并使用以下 mailcap 条目

text/html; html2text --images-to-alt --ignore-tables --decode-errors=ignore %s; copiousoutput

查看 pdf 文件

您还可以使用类似于 mailcap HTML 视图的配置查看附加的 pdf 文件。

例如,要使用 zathura 打开,请将以下行添加到 mailcap 文件

application/pdf; zathura %s;

然后在附件部分,在 pdf 文件上键入 ENTER,zathura 的一个实例将打开该文件。

这可以使用其他 pdf 查看器完成。

过滤消息视图

您可以使用 limit 命令(默认快捷键:l)将视图限制为与模式和特定属性匹配的电子邮件。

要查看标题中包含 "foo" 的所有电子邮件,只需写入 "foo" 即可。要删除过滤器,请使用 "all" 关键字。

要查看所有标记的消息,请使用

~F

要查看所有未读消息,这些消息的大小 ≥1MB 或来自 johndoe,请使用

~U (~z 1M- | ~f johndoe)

所有可能的模式都列在官方手册中。

对话分组

默认排序顺序是按日期。使用 sort-mailbox 命令(默认键:o)更改排序选项。您可以按对话/线程对电子邮件进行分组,在这种情况下,您可以定义如何对线程进行排序以及如何在线程内进行排序。

在以下示例中,线程根据其最后一封电子邮件的日期排序。

muttrc
set sort=threads
set sort_aux=last-date-received

配置编辑器以与 mutt 配合使用

vim

  • 要将文本宽度限制为 72 个字符,请编辑您的 .vimrc 文件并添加
au BufRead /tmp/mutt-* set tw=72
  • 另一种选择是使用 Vim 的 mail 文件类型插件来启用 72 字符宽度以外的其他以邮件为中心的功能。编辑 ~/.vim/filetype.vim,如果不存在则创建它,并添加
augroup filetypedetect
  " Mail
  autocmd BufRead,BufNewFile *mutt-*              setfiletype mail
augroup END
  • 要设置不同的 tmp 目录,例如 ~/.tmp,请在您的 muttrc 中添加一行,如下所示
set tmpdir="~/.tmp"
  • 要重新格式化修改后的文本,请参阅 Vim 上下文帮助
:h 10.7

GNU nano

nano 是另一个与 Mutt 一起使用的不错的控制台编辑器。

要将文本宽度限制为 72 个字符,请编辑您的 .nanorc 文件并添加

 set fill 72
 set breaklonglines

如果您不想全局限制文本宽度,可以将列号作为参数传递给 muttrc 文件中的 hard-wrap 选项,例如

 set editor="nano -r 72 -b"

此外,在 muttrc 文件中,您可以指定开始编辑的行,以便跳过邮件标头

 set editor="nano +7"

Emacs

Emacs 具有mailmessage 主要模式。要使 Emacs 从 Mutt 调用时自动切换到 mail-mode,您可以将以下内容添加到您的 .emacs

.emacs
;; Mutt support.
(add-to-list 'auto-mode-alist '("/tmp/mutt.*" . mail-mode))
;; Neomutt support.
(add-to-list 'auto-mode-alist '("/tmp/neomutt-" . mail-mode))

如果您通常运行 Emacs 守护程序,您可能希望 Mutt 连接到它。将此添加到您的 .muttrc

.muttrc
set editor="emacsclient -a \"\" -t"

显示设置

颜色

追加 /usr/share/doc/mutt/samples/colors.linux 的内容到您的 .muttrc 文件,或复制并 source 它。然后根据您的喜好进行调整。

这些设置中的每一个将产生的实际颜色取决于您的 ~/.Xresources 文件中设置的颜色。

或者,您可以 source 任何您想要的文件,其中包含颜色(从而充当主题文件)。有关主题示例,请参阅 [3]

索引格式

以下是一个快速示例,可放入您的 .muttrc 中以自定义索引格式,即文件夹视图中显示的列。

set date_format="%y-%m-%d %T"
set index_format="%2C | %Z [%d] %-30.30F (%-4.4c) %s"

有关更多详细信息,请参阅Mutt 参考strftime(3)printf(3)

在“已发送”文件夹视图中显示收件人而不是发件人

默认情况下,Mutt 在 index_format 变量中使用 %L 格式字符串,这将显示

  • “To <list-name>”,如果“To:”或“Cc:”标头字段中的地址与用户 subscribe 命令定义的地址匹配。
  • 否则,如果消息来自您,则显示作者姓名,否则显示收件人姓名。

如果您在同一邮箱中使用多个电子邮件地址,请确保配置 alternates 变量,以便 Mutt 知道哪些消息来自您。

muttrc
alternates    ^me@example.COM$ ^me@example.NET$ ^example@archlinux.ORG$

可变列宽

如果您调整窗口大小,主题可能会被截断,而某些字段(如发件人)仍有未使用的空间。您可以使用 shell 调用 tput cols 获取终端支持的最大列数(即宽度)。使用此值,您可以为“发件人”和“主题”等字段设置宽度的百分比。

使用上述 folder-hook 和 24 像素的侧边栏宽度的示例

muttrc
## From field gets 30% of remaining space, Subject gets 70%.
## Remaining space is the total width minus the other fields (35), minus the sidebar (24)
set my_index_format_pre='set my_col_from = `echo $((30 * ($(tput cols)-35-24) / 100))`; set my_col_subject = `echo $((70 * ($(tput cols)-35-24) / 100))`; set index_format="%2C | %Z [%d] %-$my_col_from.${my_col_from}'
set my_index_format_post=' (%-4.4c) %?M?<%M> ?%-$my_col_subject.${my_col_subject}s"'

folder-hook .*[sS]ent.* "$my_index_format_pre"t"$my_index_format_post"
folder-hook ! .*[sS]ent.* "$my_index_format_pre"F"$my_index_format_post"

我们必须从 hook 中设置变量 my_col_frommy_col_from。否则,列值将不会重新计算。

我们可以添加一个绑定来强制重新计算索引格式,而无需更改文件夹

muttrc
macro index,pager \CL "<enter-command>$my_index_format_pre"F"$my_index_format_post<enter><redraw-screen>"

侧边栏

侧边栏的示例设置位于 /usr/share/doc/mutt/samples/sample.muttrc-sidebar 中,包括按键绑定。复制、编辑并在您的 mutt 配置文件中 source 该文件。确保更改 set sidebar_visible = yes

追加 以下内容以切换侧边栏的可见性

bind index,pager B sidebar-toggle-visible
注意:必须设置 mailboxes 变量或 imap_check_subscribed 以告诉侧边栏应显示哪个文件夹。请参阅 mailboxes 部分。

请注意,使用 mailboxes 选项,如果不使用 sidebar_sort_method 选项,文件夹将按照它们设置为 mailboxes 的顺序出现。

提示: 要在不同的邮箱之间添加分隔符,请向文件夹列表添加一个虚拟文件夹。例如,添加
mailboxes "+-- My mailbox -----------"

在分页器视图上方显示索引

在您的 muttrc 中设置以下变量

set pager_index_lines=10

联系人管理

地址别名

别名是 Mutt 管理联系人的方式。别名是 nickname [longname] <address>

  • nickname 是您在 Mutt 中键入以获取联系人地址的内容。只能是一个单词,并且应该易于记忆。
  • longname 是可选的。它可以是多个单词。
  • <address> 必须是有效格式(即带有 @)。

它确实非常简单。将此添加到 .muttrc

set alias_file = "~/.mutt/aliases"
set sort_alias = alias
set reverse_alias = yes
source $alias_file

说明

  • alias_file 是当您从 Mutt 中添加别名时,信息存储的文件。
  • sort_alias 指定在 Mutt 中显示别名列表时要使用的排序字段。可能的值:alias、address。
  • reverse_alias 如果设置为 yes,mutt 将在索引菜单中显示来自您的别名的“个人”姓名(如果它找到与消息发件人匹配的别名)。
  • source $alias_file 告诉 Mutt 在启动时读取别名。自动完成需要此项。

现在,当提示 To: 时,您只需写入别名而不是完整地址即可。这样做的好处是您可以使用 Tab 键自动完成别名。自动完成错误或空字符串将显示完整列表。您可以像往常一样选择别名,也可以通过键入其索引号来选择。

有两种创建别名的方法

  • 在 Mutt 中,如果选择了目标人员的电子邮件,请按 a
  • 手动编辑 alias_file。语法非常简单
alias nickname Long Name <my-friend@domain.tld>

Abook

abookAUR 是一个独立的程序,专门用于联系人管理。它使用非常简单的基于文本的界面,联系人存储在纯文本、人类可读的数据库中。此外,所需的联系人属性是可扩展的(生日、地址、传真等)。

Abook 专门设计为与 Mutt 接口,以便它可以作为 Mutt 内部别名的完整、功能更丰富的替代品。如果您想使用 Abook 代替别名,请删除 .muttrc 中的别名配置并添加此内容

muttrc
## Abook
set query_command= "abook --mutt-query '%s'"
macro index,pager  a "<pipe-message>abook --add-email-quiet<return>" "Add this sender to Abook"
bind editor        <Tab> complete-query

有关更多详细信息和完整的配置示例,请参阅 abook(1)abookrc(5)

Goobook

Goobook 允许您从命令行或 Mutt 内部搜索您的 Google 联系人,并且可以使用 goobookAURgoobook-gitAUR 软件包安装。

在使用 goobook 之前,您必须配置 ~/.goobookrc。要生成默认模板

$ goobook config-template > ~/.goobookrc

有关配置选项,请参阅 ~/.goobookrc。至少,您需要输入您的电子邮件密码

注意: 如果您启用了 Google 帐户的两步验证,您可能需要生成一个应用密码。

为了通过 Google 身份验证,您必须生成客户端 ID 和客户端密钥。有关详细信息,请参阅 [4]

如果您想使用 Goobook 代替别名,请删除 .muttrc 中的任何别名配置并添加

muttrc
## GooBook
set query_command="goobook query '%s'"
macro index,pager a "<pipe-message>goobook add<return>" "add sender to google contacts"
bind editor <Tab> complete-query

在 Mutt 中撰写电子邮件消息时,Tab 键现在将搜索您的 Google 联系人。查看消息时,a 键会将发件人添加到 Google 联系人。

Khard

khard 是一个命令行地址簿,它使用本地存储的 carddav 地址簿条目。您可以使用 vdirsyncer 将这些条目与 CardDAV 服务器同步。

在 mutt 中的集成类似于 abook,请参阅 khard 文档

管理多个发件人帐户

如果您使用多个发件人帐户,您可以自动将特定的发件人帐户与收件人关联。mutt-vidAUR 扫描已发送的电子邮件,查找与特定收件人关联的最新“发件人”详细信息,并将这些信息保存在文件中供 mutt 源使用。下次您向此收件人发送电子邮件时,mutt 将自动调用一个 send-hook,其中包含您之前使用的相同电子邮件地址和真实姓名。有关更多详细信息,请参阅 mutt-vid 的 主页

手动请求 IMAP 邮件检索

如果您不想等待下一次自动 IMAP 获取(或者如果您没有启用它),您可能希望手动获取邮件。有一个 mutt 命令 imap-fetch-mail 用于此目的。或者,您可以将其绑定到一个键

bind index "^" imap-fetch-mail

避免由于着色导致大型 (IMAP) 文件夹索引缓慢

通过正则表达式进行索引高亮显示很好,但如果您的正则表达式检查消息正文,可能会导致文件夹查看速度变慢。

使用 folder-hook 仅在例如收件箱中进行高亮显示(如果您设法有效地清空您的邮箱)

folder-hook . 'uncolor index "~b \"Hi Joe\" ~R !~T !~F !~p !~P"'
folder-hook ""!"" 'color index brightyellow black "~b \"Hi Joe\" ~N !~T !~F !~p !~P"'

加快文件夹切换速度

将此添加到您的 .muttrc

set sleep_time = 0

存档已处理的电子邮件

当您阅读电子邮件时,您有四个选择:回复、标记、存档或删除。如果您心中有此想法,您可以使用以下宏保持收件箱简洁明了(为 Gmail 设置)

macro index \' "<tag-pattern>~R !~D !~F<enter>\
<tag-prefix><save-message>+[Gmail]/All <enter>" \
"Archive"

将邮件从一台计算机迁移到另一台计算机

如果您要将邮件传输到新机器(复制粘贴),您可能需要删除标头缓存(如果您按照上述配置,则为类似 ~/.cache/mutt 的文件或文件夹),以使 Mutt 能够读取您迁移的电子邮件。否则,Mutt 可能会冻结。

请注意,如果您为标头缓存创建了一个文件夹,则所有邮箱都将拥有自己的缓存文件,因此您可以单独删除缓存,而无需删除所有内容。

保存附件的默认文件夹

默认情况下,Mutt 会将附件保存到它启动的文件夹。如果您想始终将默认目标设置为 ~/attachments,您可以创建以下别名,该别名在此文件夹中启动 Mutt

alias mutt='cd ~/attachments && mutt'

如果您使用 bash,您可以使用 pushdpopd 在 mutt 退出后恢复目录状态。

alias mutt='pushd ~/attachments; mutt; popd'

分页器行为

转到下一页时显示上下文行

set pager_context=3

在末尾停止,而不是显示下一封邮件

set pager_stop=yes

快速回复

默认情况下,当您回复电子邮件时,Mutt 会要求确认收件人和主题。它还会询问您是否要将原始邮件包含在您的回复中。如果您假设您将始终坚持默认值,则可以设置 Mutt 以跳过这些问题

muttrc
set fast_reply=yes
set include=yes

您仍然可以在发送前编辑收件人和主题。

从 group-reply 中忽略自己的电子邮件地址

当您 group-reply 回复一封您被抄送的邮件时,Mutt 会将您的电子邮件地址包含在收件人列表中。您可以要求 Mutt 使用以下命令忽略某些地址

alternates mail1@server1|mail2@server2|...

IMAP 消息缓存

使用内置 IMAP 支持时,默认情况下电子邮件会在内存中获取。多次检索大型电子邮件将多次从您的 IMAP 服务器下载它。

或者,您可以要求 Mutt 将所有获取的消息存储在磁盘上

muttrc
set message_cachedir=~/.cache/mutt/messages

(文件夹必须存在。)这将使任何未来的检索都变得瞬间完成,即使是大型附件也是如此。

如果您想从缓存中清除超过 50MB 限制的最旧电子邮件,您可以使用如下脚本

~/.mutt/purgecache.sh
#!/bin/sh

## In KB.
CACHE_LIMIT=51200

cd "$1" 2>/dev/null
[ $? -ne 0 ] && exit

[ $(du -s . | cut -f1 -d'	') -lt $CACHE_LIMIT ] && exit
while IFS= read -r i; do
	rm "$i"
	[ $(du -s . | cut -f1 -d'	') -lt $CACHE_LIMIT ] && exit
done <<EOF
$(find . -type f -exec ls -rt1 {} +)
EOF

并在启动时调用它

muttrc
set message_cachedir=~/.cache/mutt/messages
source "~/.mutt/purgecache.sh '$message_cachedir'|"

在后台打开附件或查看 HTML 电子邮件

默认情况下,在 mutt 中打开附件会阻止 mutt。您可以编辑 ~/.mutt/mailcap 以追加一个 &,以便 mutt 将在后台启动默认应用程序之一。以 xdg-open 为例,

text/html; xdg-open %s &> /dev/null &; nametemplate=%s.html
application/*; xdg-open %s &> /dev/null &; 
image/*; xdg-open %s &> /dev/null &;

&> /dev/null 用于防止任何错误或消息弄乱 mutt 终端窗口。nametemplate=%s.html 用于因为 chromium 拒绝将没有 .html 扩展名的本地文件渲染为 HTML。

故障排除

退格键在 Mutt 中不起作用

这是某些类似 xterm 的终端的常见问题。两种解决方案

  • 或者在 .muttrc 中重新绑定键
bind index,pager ^? previous-line

请注意,^? 是一个单字符,表示 插入符号表示法中的退格键。要在 Emacs 中键入,请使用 Ctrl+q Backspace,在 Vim 中使用 Ctrl+v Backspace

  • 或者修复您的终端
$ infocmp > termbs.src

编辑 termbs.src 并将 kbs=^H 更改为 kbs=\177,然后

$ tic -x termbs.src

change-folder 功能始终提示相同的邮箱

这不是错误,这实际上是预期的行为。有关解决方法,请参阅多个帐户部分

使用 Mutt 只读模式 (Mutt -R) 时无法更改文件夹

这肯定是由于您使用了如下宏

macro index,pager <f2> '<sync-mailbox><enter-command>source ~/.mutt/personal<enter><change-folder>!<enter>'

此宏告诉 Mutt 在切换之前同步(这是一个写入操作)。

或者使用侧边栏或设置另一个宏

macro index,pager <f3> '<enter-command>source ~/.mutt/personal<enter><change-folder>!<enter>'

发送消息时出错,子进程退出,代码为 127 (Exec error.)

这是一个 SMTP 错误。这意味着 mutt 不知道如何发送消息。您可以尝试安装 sendmail 并查看是否解决了您的问题,或者您可以设置 smtp_url 变量。如果您使用 gmail,您可以将以下内容添加到您的 muttrc,以告诉 mutt 使用 gmail 的 smtp 服务器。

set smtp_url=smtps://$imap_user:$imap_pass@smtp.gmail.com

请注意 smtps 协议,这很重要。这应该可以解决问题。

字符编码问题

如果您遇到字符编码问题,请首先阅读 Mutt wiki 中的此部分

如果中文字符仍然乱码,即使标头中指定了 GB2312,也可能有助于使用 GBK 解码。您可以使用 iconv 通过将以下内容添加到您的 mailcap 文件来完成此操作

text/plain; iconv -f gbk -t utf-8 %s; test=echo "%{charset}" | grep -ic "gb2312"; copiousoutput;

并通过在您的 .muttrc 中添加一行来启用它

auto_view text/plain

对于 HTML 电子邮件,您可以通过将 mailcap 的相关行中的 %{charset} 替换为 $(echo %{charset} | sed s/gb2312/gbk/I) 来进行编辑,例如

text/html; w3m -dump -I $(echo %{charset} | sed s/gb2312/gbk/I) %s; nametemplate=%s.html; copiousoutput

无法使用 GMail 登录

Gmail 禁用了来自其认为安全性较低的应用程序(包括 mutt)的访问。您可以通过按照此处的说明启用访问

无法使用 urlview 打开过长的 URL

过长的 URL 无法正确解析,因为 urlview 不解码文本(请参阅 [5])。您可以让 mutt 解码电子邮件。将打开 urlview 的行替换为以下代码

macro index \cb "\
:set my_tmp_pipe_decode=\$pipe_decode\n\
:set pipe_decode\n\
|urlview\n\
:set pipe_decode=\$my_tmp_pipe_decode\n\
:unset my_tmp_pipe_decode\n" \
'call urlview to extract URLs out of a message'

另一种选择是使用 urlscan 代替,它提供了一个文本用户界面来选择 URL,并且可以处理更奇怪的文本格式。

文档

新手可能会发现很难找到 Mutt 的帮助。实际上,大多数主题都在官方文档中涵盖。我们敦促您阅读它。

  • 官方手册。Arch Linux 的股票 mutt 软件包还在 /usr/share/doc/mutt/ 安装了 HTML 和纯文本手册。
  • muttmuttrc 手册页。

另请参阅