Using offlineimap and Dovecot IMAP server with multiple Google Accounts

2012/04/29

For a long time I have been using offlineimap to sync my emails with various email accounts and using Wanderlust for Emacs as my mail client. However, Wanderlust seems to be have a number of annoying issues. Plus I've been reading about how cool gnus is, so I thought I would give that a try. The main thing that has kept me using Wanderlust over gnus is the excellent maildir support. However, many people have been using a local IMAP server with gnus. All of the information I can seem to find online assumes you only have one email account to sync up. Recently a thread on the Org-Mode mailing list pointed me to configuring Dovecot so that I can now serve up multiple email accounts! So here is my config, using Dovecot on (K)Ubuntu 11.10/12.04:

Dovecot Config

Give Dovecot a location to store emails. The %u tells Dovecot to give each user their own directory.


mail_location = maildir:/home/Maildir/%u

passdb
{ args = /etc/dovecot/passwd driver = passwd-file } 

In the passwd file, for each email account give it a separate username:


user1:{plain}password:1000:1000::/home/::userdb\_mail=maildir:/home//Maildir/user1
user2:{plain}password:1000:1000::/home/::userdb\_mail=maildir:/home//Maildir/user2

Offlineimap Config

Next we have to tell offlineimap to collect mail from a remote IMAP server. Below is an example config for one email account. For multiple accounts you would have multiple accounts and repositories defined in the offlineimap config file. For each remote repository, use a separate local repository with the corresponding local username you gave in the passwd file above.


[Account Account1]
localrepository=LocalIMAP1 
remoterepository=RemoteIMAP1 

[Repository LocalIMAP1] 
type = IMAP 
remotehost=localhost 
remoteuser= 
remoteuser=

[Repository RemoteIMAP1] 
type=IMAP 
remotehost=imap.gmail.com
remoteuser=<username>@gmail.com 
remotepass=<password>
ssl=yes 
realdelete=no 

The above sets up offlineimap for one gmail account. Offlineimap will sync up Gmail with the local Dovecot instance. You can then access your email by connecting to localhost via Gnus/Mutt etc.