Feb 27, 2009

Mutt tutorial | Mutt Configuration

Thank Andrew, Mark Stosberg for their insightful Mutt configuration web page.

Mutt is a text based email client. It doesn't have fancy interfaces like outlook, thunderbird. But indeed it is the most handy and configurable email client I've ever used. I've been using Mutt for a month, which is a tough month because I have to configure this and that until I feel comfortable using it.

Unlike other email clients, to use Mutt, you have to install at least two programs other than Mutt. One is for getting emails from server, another is for sending emails. In this tutorial, I use fetchmail to get email and msmtp to send mail. I also will introduce procmail, which is used to sort your emails into different mailboxes, which is extremely efficient if you have a large number of emails to read.

OK, let's get started.

1 Install

First of, you need to install all of the programs. In Debian based Linux OS, you just need to type the following command in a terminal

sudo apt-get install mutt fetchmail msmtp procmail

At this point, you'll see nothing when running mutt. That's because nothing has been set up, so mutt doesn't know where the mailboxes are. Therefore, our first goal is to receive the emails and save them to your computers. (I'm not a big fan of saving emails on the server, so I prefer downloading them to my hard drives and archiving old emails once a while)

After install, you need to set your email file in your system, and any incoming email are saved there. Let's say it's in the /home/username/mail/inbox file. To do this, simply create the folder in your home directory and add the following line in your .bashrc file.

MAIL=/home/username/mail/inbox && export MAIL

2 Receive Emails

2.1 fetchmail

Fetchmail can fetch your emails from server via POP3 and IMAP protocols, and It's pretty easy to set up.

First, create a file named .fetchmailrc in your home directory, and change the permission of this file by running following command (because you probably will write your password in this file).

chmod 700 .fetchmailrc

Then add the following lines to this file.

poll pop.gmail.com with proto POP3
user 'a@gmail.com' there with password 'aaa' is 'usrname' here options stripcr ssl sslcertck sslcertpath '/home/usrname/mail/certs/' keep
mda '/usr/bin/procmail -d %T'

poll pop.gmail.com with proto POP3
user 'b@gmail.com' there with password 'bbb' is 'usrname' here options stripcr ssl sslcertck sslcertpath '/home/usrname/mail/certs/' keep
mda '/usr/bin/procmail -d %T'

Change "usrname" to your Linux username. Note that I included ssl certificate check. The certificate file, as shown above, in located in your /home/username/mail/certs/ directory.

The certificates you need if you are setting up Gmail are Equifax_Secure_CA.pem and Thawte_Premium_Server_CA.pem. create the certificate files in your certs/ folder:

cd ~/mail/certs
touch Thawte_Premium_Server_CA.pem
touch Equifax_Secure_CA.pem

Then add these to your Thawte_Premium_Server_CA.pem:


and these to your Equifax_Secure_CA.pem:


Then let the system know where the certificates are by running

$ c_rehash $HOME/mail/certs/

2.2 procmail

Since this tutorial is getting longer and longer, I'll explain procmail in a separate post. For now, all your mail will go to your $HOME/mail/inbox file.

3 Send mail

Create .msmtprc file in your home directory. Add following lines to this file.

account gmail
host smtp.gmail.com
port 587
from a@gmail.com
tls on
tls_starttls on
tls_trust_file /home/usrname/mail/certs/Thawte_Premium_Server_CA.pem
auth on
user a
password aaa

account gmail1
host smtp.gmail.com
port 587
from b@gmail.com
tls on
tls_starttls on
tls_trust_file /home/usrname/mail/certs/Thawte_Premium_Server_CA.pem
auth on
user b
password bbb

account default: gmail

Then change the permission as well:

chmod 700 $HOME/.msmtp

4 Configure Mutt

Create a file .muttrc in your home directory. Then add following to this file. A complete configuration file can be downloaded here

# Default setting

set realname = "Your Name"
set from = "a@gmail.com" #Default account
set status_format="-%r-Gmail: %f [Msgs:%?M?%M/?%m%?n? New:%n?%?o? Old:%o?%?d?\
Del:%d?%?F? Flag:%F?%?t? Tag:%t?%?p? Post:%p?%?b? Inc:%b?%?l? %l?]---(%s/%S)-%>-(%P)---"

set use_from
set envelope_from ="yes"
set nomark_old
#set signature="~/.signature"

set sendmail="/usr/bin/msmtp"

set spoolfile = ~/mail/inbox

# Folders

set folder="$HOME/mail" # Local mailboxes stored here
set postponed="+postponed" # Where to store draft messages
set mbox_type=mbox # Mailbox type
set record="+/sent/sent-mail-`date +%Y-%m`"

mailboxes ! =work =play

set sort_browser=size

ignore *
unignore Date: From: User-Agent: X-Mailer X-Operating-System To: \
Cc: Reply-To: Subject: Mail-Followup-To:
hdr_order From: Date: To: Cc: Reply-To: User-Agent: X-Mailer X-Operating-System Subject:

# Editor

set editor="vim -c 'set tw=70 et' '+/^$' "
set edit_headers=yes # See the headers when editing

# Aliases

set alias_file = ~/mail/.mutt_aliases # In their own file
source ~/mail/.mutt_aliases # Source them
set sort_alias=alias # Sort alphabetically

# Colours: defaults are a little bleak so experiment!

source ~/mail/.colors # In their own file

# Lists

subscribe mutt-users
set followup_to=yes # Sets 'Mail-Followup-To' header
set honor_followup_to=yes
fcc-hook mutt =mutt #See own posts

# Compose

set markers # mark wrapped lines of text in the pager with a +
set smart_wrap # Don't wrap mid-word
set pager_context=5 # Retain 5 lines of previous page when scrolling.
set status_on_top # Status bar on top.
push # Shows mutt version at startup

set delete=yes
set sendmail_wait=-1
set forward_format="Fwd: %s"
set query_command="lbdbq '%s'"
set include # include original message
set fast_reply # No waiting after send
set wait_key=yes
set abort_nosubject=no
set abort_unmodified=no

# Macros & Bindings

macro index,pager y ? "See mailbox list"
macro index,pager I ' fetchmail -v' "Fetchmail"
macro index,pager n '?'\
"Switch to next mailbox"

bind index,pager v next-unread-mailbox
bind index,pager V view-attachments
bind index ^N next-page
macro index d 's=archive'
macro index A 's=important'
bind index,pager ^D delete-message

bind browser v check-new
bind browser i exit
bind browser select-entry
bind browser view-file

bind index B bounce-message

# Set multiple accounts

set reverse_name=yes

alternates "aaa@gmail.com|bbb@gmail.com"

macro index \e1 ":set signature=~/.signature_private\n:\
set from=aaa@gmail.com\n:set status_format=\"-%r-Gmail: %f \
[Msgs:%?M?%M/?%m%?n? New:%n?%?o? Old:%o?%?d? Del:%d?%?F? \
Flag:%F?%?t? Tag:%t?%?p? Post:%p?%?b? Inc:%b?%?l? %l?]---(%s/%S)-%>-(%P)---\"\n"\
"Switch to Gmail"

macro index \e2 ":set signature=~/.signature_fake\n:\
set from=bbb@gmail.com\n:set realname=Jason\n:set status_format=\"-%r-Gmail2: %f \
[Msgs:%?M?%M/?%m%?n? New:%n?%?o? Old:%o?%?d? Del:%d?%?F? \
Flag:%F?%?t? Tag:%t?%?p? Post:%p?%?b? Inc:%b?%?l? %l?]---(%s/%S)-%>-(%P)---\"\n"\
"Switch to Gmail2"

# Print

set print = yes
set print_command="muttprint"

# Html autoview
# Note: turn off autoview if you have Chinese google
# mailgroup/maillist

#auto_view text/html

# Hook config

send-hook aaa@163.com$ 'my_hdr From: Good Guy '
send-hook aaa@163.com$ 'set signature="~/.signature_private"'

# Misc

unset confirmappend # No confirmation after save

I suggest you read this line to line. The configuration is pretty self-explanatory. Note that I put my mutt color config. in another file and sourced it in .muttrc file. Also note that I have set up lots of custom-keybinding, you can change that if you want - Mutt is pretty good at custom configuration. The action defined after every keybinding set up can be found in Mutt it self. Just hit "?", you can see all the key bindings.

Basically, the most used keys are:
  • j: move to next unread email
  • k move to previous unread email
  • SPACE read current email
I'll write tutorials on color, procmail, and multiple accounts setup later.

5 Acknowledgment and useful links

Using Mutt with Gmail
The Mutt Cheatsheet
Learning Mutt
Mark's Mutt Fan and Tip page

6 Related Posts

Multiple accounts in Mutt


Anonymous said...

>>> chmod 700 $HOME/.msmtp <<<
should read:
>>> chmod 700 $HOME/.msmtprc <<<

bjswift said...

I just got this...

msmtp: /home/usrname/.msmtprc: must have no more than user read/write permissions

so it seems my setup (Ubuntu 11.10 and whatever mutt version I just installed) only wants me to chmod 600... o.0

Anonymous said...

Just wanna let you know that you rock sir!

Anonymous said...

Thank you! It's very helpful!

Welcome to Non-geek's
Linux Handbook!

or if you will, Linux Cookbook.

tutorials on Linux applications~

Subscribe if you are willing to follow.

If you find the post useful, please click the donate button on the upper right corner. Any amount is useful.