Setup Mutt For Gmail

Mutt is a text based mail client. It replaces GUI based programs like Thunderbird on a headless Raspberry Pi for instance. Some people even prefer it over the common GUI based mail programs because of its simplicity and the lack of distracting graphical eye candy.
It may take a little getting used to in order to be able work with. But once you get the hang of it you can just as easily read, write and reply mails using Mutt as you can do with any other mail program.

This guide will show you how to set up Mutt for use with a Gmail account. It won't teach you how to use Mutt though. Others are probably far better in explaining how it works than me.

As always we will start by installing the program using the familiar command line below:

sudo apt-get install mutt

After that we have to create a few directories and a file in our home directory, which mutt requires:

mkdir -p ~/.mutt/cache/{headers,bodies}
touch ~/.mutt/certificates

Please note that the main .mutt directory starts with a dot, which makes it a hidden directory.

Then create a file named .muttrc (dot muttrc, a hidden file) in your home directory and copy/paste the next block of text into this file.

# A basic .muttrc for use with Gmail

# Change the following six lines to match your Gmail account details
set imap_user = "[email protected]"
set imap_pass = "PASSWORD"
set smtp_url = "smtp://[email protected]:587/"
set smtp_pass = "PASSWORD"
set from = "[email protected]"
set realname = "YOUR NAME"

# Change the following line to a different editor you prefer.
set editor = "nano"

# Basic config, you can leave this as is
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set imap_check_subscribed
set hostname = gmail.com
set mail_check = 120
set timeout = 300
set imap_keepalive = 300
set postponed = "+[GMail]/Drafts"
set record = "+[GMail]/Sent Mail"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set include
set sort = 'threads'
set sort_aux = 'reverse-last-date-received'
set auto_tag = yes
ignore "Authentication-Results:"
ignore "DomainKey-Signature:"
ignore "DKIM-Signature:"
hdr_order Date From To Cc
alternative_order text/plain text/html *
auto_view text/html
bind editor <Tab> complete-query
bind editor ^T complete
bind editor <space> noop

# Gmail-style keyboard shortcuts
macro index,pager y "<enter-command>unset trash\n <delete-message>" "Gmail archive message"
macro index,pager d "<enter-command>set trash=\"imaps://imap.googlemail.com/[GMail]/Bin\"\n\
<delete-message>" "Gmail delete message"
macro index,pager gi "<change-folder>=INBOX<enter>" "Go to inbox"
macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
macro index,pager gs "<change-folder>=[Gmail]/Starred<enter>" "Go to starred messages"
macro index,pager gd "<change-folder>=[Gmail]/Drafts<enter>" "Go to drafts"

#end-----------------------------------------

Naturally you will have to change the first 6 lines starting with set to reflect your own Gmail account. In fact you will have to change all capitalized words with your own information.

Mutt will use the IMAP protocol, which means that all mail sent and received will be left on the Gmail server, unless you specifically choose to delete it. This means that you can still read your conversations on other computers afterwards.

That's all you need to do to make it work. However let's improve our safety a bit, just in case your Raspberry Pi is used by multiple users.

Extra Security Measures

Gmail is by design a lot safer than the average mail account you get from your local ISP because all network traffic to and from the Gmail server is encrypted. Probably 9 out of 10 ISP mail services still use unencrypted connections, even today. But with Gmail we don't have to worry about that.

If your Raspberry Pi can be used by multiple users you would probably want to restrict permission to your Gmail folders and config file. You simply do that using the following commands:

chmod 600 .muttrc
chmod 700 .mutt

However this won't prevent the root user from looking at your files. There's nothing we can do about that. But we've got another ace up our sleeves. If you set up your Gmail account to have two step authentication you can generate an application specific password for Mutt, which you can feed to the .mutt config file. Even if root steals your password he/she still can't access your Google account, so he/she can't get to your Drive, Calender and profile settings for instance.