Geek: How to use offlineimap and the dovecot mail server to read your Gmail in Emacs efficiently

Posted: - Modified: | emacs, geek
2015-12-24: Updated with config changes now that I’m back to using Gnus+dovecot+offlineimap+Gmail
2014-04-09: This post is from 2008. =) I think I used dovecot+offlineimap because Gnus and maildir weren’t getting along properly and directly connecting with IMAP to Gmail’s server was slow, but things have probably changed a fair bit since then. I eventually moved to using the Gmail web interface for most things, but I still miss my Gnus setup!
  1. Make sure you’ve set up Postfix or some other mail server that can send mail.
  2. Install dovecot (IMAP server) and offlineimap (IMAP synchronization). You can probably find binaries for your distribution.
  3. Edit /etc/dovecot/conf.d/10-mail.conf and set the following:
    mail_location = maildir:~/Maildir:LAYOUT=fs
  4. Use the instructions fromĀ http://unix.stackexchange.com/questions/44214/encrypt-offlineimap-password to set up GPG-encrypted passwords. (optional)
  5. Put the following in ~/.offlineimaprc, changingĀ the details as needed.
    [general]
    accounts = SachaGmail
    pythonfile = ~/bin/offlineimap-password.py
    
    [Account SachaGmail]
    localrepository = Local
    remoterepository = Gmail
    status_backend = sqlite
    
    [Repository Local]
    type = Maildir
    localfolders = ~/Maildir
    
    [Repository Gmail]
    type = Gmail
    maxconnections = 2
    remoteuser = sacha@sachachua.com
    realdelete = no
    folderfilter = lambda foldername: foldername in ['INBOX', '[Gmail]/All Mail', '[Gmail]/Sent Mail', '[Gmail].Starred', '[Gmail].Drafts', '[Gmail].Important']
    nametrans = lambda folder: re.sub('^INBOX$', '[Gmail].Inbox', folder)
    sslcacertfile = /etc/ssl/certs/ca-certificates.crt
    remotepasseval = mailpasswd("gmail")
    

    If you feel comfortable specifying your password in your ~/.offlineimaprc, you can do so by changing remotepasseval to remotepass. If so, you don’t need the pythonfile line.

  6. chmod go-rwx ~/.offlineimaprc for a little bit of safety.
  7. Type offlineimap to start synchronizing.
  8. While that’s synchronizing, use something like this as your ~/.gnus:
    (setq gnus-select-method
          '(nnimap "Mail"
    	       (nnimap-address "localhost")
    	       (nnimap-stream network)
    	       (nnimap-authenticator login)))
    
    (setq user-mail-address "youremail@example.com")
    (setq gnus-ignored-from-addresses "youruser")
    
  9. Start Emacs. Start Gnus with M-x gnus. If you don’t see the INBOX group, press ^ (gnus-group-enter-server-mode), open nnimap:Mail, move your cursor to the INBOX, and either press RET to go into the group or press u (gnus-browse-unsubscribe-current-group) to toggle the subscription status until you’re subscribed to the group. Then it should show up on the group screen (M-x gnus).

Hope that helps. Have fun!

You can view 6 comments or e-mail me at sacha@sachachua.com.

6 comments

I had a problem with the summary buffer showing the wrong subjects for certain messages (entering them would give the right message though). It seems gnus-agent was at fault (or gmail's interactions with it). http://www.emacswiki.org/em... gave the solution, just disable it for gmail.

Wow Sacha, I like this configuration very much! To tell the truth, I haven't really got this the first time around, since I'm fairly new to the emacs universe. Later on, it came to me that I really haven't started the dovecot process, which causes gnus to block emacs' io events. I think you should also mention that the dovecot process should be up and running since other linux distros don't automatically start dovecot upon installation. It will sure help a lot of newbies out there. Thanks a lot!

Rumour has it that the new Gnus in Emacs 24 is much faster at doing IMAP, so you won't need this in order to do IMAP mail with, say, Google mail. Might still be useful for offline use, though. =)

Chris Barrington-Leigh

2011-12-01T01:05:21Z

Wow, this is super! I've been looking for years for someone to spell these out together.

How do you deal with deleting (archiving) messages? With drafts? With labels?
so that they sync with Gmail's version nicely?

Thanks,
c

Olivier Berger

2013-08-23T14:32:32Z

See also Roland's setup : http://roland.entierement.n...

Olivier: Great, thanks! =)