Using Mutt With GMail

I’ve been using GMail for many years, and a long time ago I used the older POP/SMTP mechanism to synchronise my mail locally. I’ve recently discovered that (probably due to the newer label functionality), that this is no longer the most appropriate thing to do. What I found was that I would end up with a lot of duplicated emails none of which were sorted in any manner, with none of the nice ordering that I’d gotten used to from using the web and Android interfaces.

With that in mind I started looking at the possibility of using the IMAP interface instead. This post describes how I needed to configure mutt to make this happen.

The first thing to note is that mutt is simply a mail client (in the truest sense.) It has no inbuilt facilities for sending or retrieving emails, and relies on external applications to perform this functionality. The two external programs I settled on after a bit of internet research are:

* offlineimap * msmtp

OfflineIMAP

Offlineimap is a python based utility capable of synchronising IMAP mails from a variety of servers.

Installing it is fairly easy, since I use Fedora it’s as simple as:

1 # sudo yum install offlineimap

All of the appropriate dependancies will get brought in automatically.

Once you have that, you need to create a .offlineimaprc file in your home directory:

 1   ui = ttyui
 2   accounts = GMail
 3 
 4   Account GMail
 5   localrepository = Gmail-Local
 6   remoterepository = Gmail-Remote
 7 
 8   Repository Gmail-Local
 9   type = Maildir
10   localfolders = ~/Mail/GMail
11 
12   Repository Gmail-Remote
13   type = Gmail 
14   remoteuser = username
15   remotepass = password
16   realdelete = no
17 
18   # line breaks added for readability
19   nametrans = lambda folder: re.sub('.\*Spam$', 'spam', 
20       re.sub('.*Drafts$', 'drafts', 
21       re.sub('.*Sent Mail$', 'sent', 
22       re.sub('.*Starred$', 'flagged', 
23       re.sub('.*Trash$', 'trash', 
24       re.sub('.\*All Mail$', 'archive', folder))))))

Generally you should just be able to set the username and password as appropriate for your account. The one caveat here is that if, like me, you have two-factor authentication switch on in GMail, then the password you enter must be one of the application specific ones rather than your master password.

Once you have this set up, you should just be able to:

# offlineimap

MSMTP

We will also need a way to send emails, and for this we can use msmtp.

Again, you will need a configuration file in your home directory (.msmtprc). Mine looks like this:

 1 account default 
 2 host smtp.gmail.com
 3 port 587
 4 protocol smtp
 5 auth on
 6 from username
 7 user username
 8 password password
 9 tls on
10 tls\_nocertcheck

You will need to change the username and password accordingly.

Mutt

Finally, with everything else configured, you can start to use mutt itself.

As with everything else, you will need to setup a configuration file (.muttrc):

 1 set realname            = "username"
 2 set from                = "email\_address"
 3 set envelope\_from
 4 set mail\_check          = 0
 5 unset move
 6 set delete 
 7 unset confirmappend
 8 set quit 
 9 unset mark\_old 
10 
11 set include
12 set attribution = "\* \%n <%a> \[\%{\%Y-\%m-\%d \%H:\%M:\%S \%Z\]}:\\n"
13 
14 # index options
15 set sort                = threads
16 set sort\_aux            = reverse-last-date-received
17 set sort\_re
18 
19 # pager
20 set pager\_index\_lines   = 8
21 set pager\_context       = 5
22 set pager\_stop
23 set menu\_scroll 
24 set smart\_wrap
25 set tilde    
26 unset markers 
27 
28 # composing mail
29 set fcc\_attach   
30 unset mime\_forward    
31 set forward\_format      = "Fwd: %s"
32 set include       
33 set forward\_quote 
34 
35 alternative\_order text/plain text/enriched text/html
36 
37 # headers to show
38 ignore *                     
39 unignore from: to: cc: date: subject:
40 hdr\_order from: to: cc: date: subject:
41 
42 # options
43 set mbox\_type   = Maildir
44 set folder      = ~/Mail/GMail
45 set spoolfile   = "+INBOX"
46 set mbox        = "+archive"
47 set postponed   = "+drafts"
48 unset record
49 
50 set sendmail    = /usr/bin/msmtp
51 
52 # mailboxes
53 mailboxes +INBOX +archive +sent +drafts +spam +trash
54 
55 # bindings
56 macro index D "<save-message>+trash<enter>"   "move message to the trash"
57 macro index S "<save-message>+spam<enter>"    "mark message as spam"
58 
59 # External config files to use
60 source ~/.mutt/colours

It’s probably a little bit more complicated than it needs to be, but it works for me!

Conclusion

Running mutt may not be everyones cup of tea, but it does give you back a lot of control that modern email clients and web based interfaces take away from you.

If need be you can always reverse SSH tunnel your way back to a “safe” machine and read your email from there without fear of compromising your emails accounts. But I’ll leave that discussion for another day…


Tags:
blog comments powered by Disqus