Outsourcing

Easy Gmail reading with mutt

Vincent Danen shows you how to make use of Gmail on the command line with mutt. Here are the basic commands to set it up.

If you're like me, you prefer command line tools over GUI tools, and for email you prefer plaintext over HTML format. You can have the best of both worlds by using Gmail on the command line with a tool like mutt, although it does take a bit of tweaking. If you prefer to keep a copy of your mail locally, you can use POP3 (via fetchmail and procmail) or something like OfflineIMAP to suck mail down and keep a local copy, syncing the mail back and forth.

With mutt, however, because later versions support IMAP and SMTP directly, you are able to work with your Gmail or Google Apps account using nothing more than mutt itself: no fetchmail, no procmail, no OfflineIMAP. All of these tools are great, but Gmail has pretty decent filtering and the advantages of using the web UI when at a computer not your own might make you prefer IMAP to sucking everything down via POP3 anyway.

Mutt is extremely straightforward when used with Gmail.

First, you need to create a ~/.muttrc with the following contents. These are really the bare-bones commands you need for mutt to work with Gmail (customize to suit passwords and email address):

# A basic .muttrc for use with Gmail
# Change the following six lines to match your Gmail account details
set imap_user = "[user]@gmail.com"
set imap_pass = "[password]"
set smtp_url = "smtp://[user]@gmail.com@smtp.gmail.com:587/"
set smtp_pass = "[password]"
set from = "[user]@gmail.com"
set realname = "[User Name]"
# Change the following line to a different editor you prefer.
set editor = "vim"
# 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 auto_tag = yes
hdr_order Date From To Cc
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 gl "<change-folder>"
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"
macro index,pager gt "<change-folder>=[Gmail]/Sent Mail<enter>" "Go to sent mail"

Next, you need to create the directories that mutt will need to store cache information, and also create the mailcap file in order to view HTML messages and attachments:

$ mkdir -p ~/.mutt/cache/{headers,bodies}
$ echo "text/html; /usr/bin/elinks -force-html %s" >~/.mailcap

Change the path to the preferred CLI HTML browser of your choice: elinks is a great browser; lynx is good also. You can also add other entries to your mailcap if you prefer; such as launching OpenOffice.org to view Word files (application/msword) or Evince for PDF files (application/pdf).

After this, you can start mutt and have immediate access to your Gmail or Google Apps account. You will be asked to save the certificate information; if you select Always Allow, you won't be prompted for it again.

This configuration is obviously quite sparse, and there are many tutorials online on how to spruce up mutt with colors and other features. This is pretty much the bare minimum to get mutt to work with Gmail; you can send mail and read mail via IMAP, which allows you to get access to your mail account from anywhere that you can ssh into. The keybindings noted also mimic some of those keyboard shortcuts used in the web UI. For the paranoid, if you omit the passwords in the configuration file, mutt will ask you each time you start it up.

Get the PDF version of this tip here.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

7 comments
hermanchen
hermanchen

great article! easy , simple to setup

ericknkili
ericknkili

worst article ever!! we are in the 21 century now, why would somebody want to use command line?

mark
mark

I've just got mutt installed as I find it a nice backup that can be used from just about anywhere or any machine to read my email, when I don't have access to a standard configured gui client.

aj3jr
aj3jr

If someone keeps their box clean(I'm not one) and they receive short msgs like "call me", "i need the usual report now", etc then cli email client would be great. Pine(not a cli but is text based) still holds fond memories for me. I loved the command line world of Unix. I'd make a subdirectory for a project. My focus would be on/in that project and I'll call my tools. I need some grep, a little awk, this needs sed, I'll just vi this, etc. There is no need to click word, wait for it to open, go to my new project folder. Let's get a little Excel in here, Click Excel and browse to the same folder that Word is currently saving new files into. Yes 'recent files' help once they're created. BUT with Unix my focus was in the folder and my work; not launching apps and going to my work. AND with 'cd -' I can have 2 project folders!! Yea!! ;) I sorry you missed that wonderful environment. :( Now days many need a gui to ftp a file :( OH, that's right windows ships with a cli ftp client even in Win7(at least it's on my Ent Ed). Thank God M$ Windoze comes with IE so people can download filezilla! Yes, I use Putty instead of M$'s telnet client. Sorry for the rant. Hope I didn't offend anyone. Yes Unix is cryptic, but once you learn it swimming in it is nice.

kprince
kprince

Because a command line does exactly what you tell it (no more, no less) without flashy graphics or paper clips. Sometimes command line is just what the doctor ordered especially for those using adaptive technology.

Marcelle Green
Marcelle Green

I have to agree somewhat with Erik. Nobody wants to take the time to do all that work to use the program. If they really want to make gmail more user friendly they have to make mutt easy to use with it. I certainly would not take the time to go through all those steps, it negates the whole purpose of having mutt: making gmail easier and more accessible.