Open Source

Keep IMAP email messages locally using OfflineIMAP

Vincent Danen discusses the uses of OfflineIMAP for synchronizing local and remote IMAP mailboxes and providing a good method for backing up your email.

Most mail clients today have really good support for email retrieved over IMAP. Today, with Web-based mail clients and multiple mail clients on different systems connecting to the same mail account, using POP3 to get mail doesn't make as much sense as it used to. IMAP, on the other hand, makes a lot more sense and makes mail available regardless of what client or computer you are using.

One extremely useful tool is OfflineIMAP. This is a python tool that synchronizes remote IMAP mailboxes to local mailboxes, allowing you to use IMAP even if your mail client doesn't support it. It is also a great tool to use to back up your email.

OfflineIMAP should be available from your distribution; in Fedora simply run "yum install offlineimap". Once this is done, edit ~/.offlineimaprc to look similar to this:

accounts = Gmail
ui = Noninteractive.Quiet
[Account Gmail]
localrepository = Local
remoterepository = Remote
[Repository Local]
type = Maildir
localfolders = ~/Mail/
[Repository Remote]
type = IMAP
ssl = yes
remotehost =
remoteuser =
remotepass = sekret

The above essentially tells OfflineIMAP to keep a local copy of your IMAP messages in ~/Mail/. The messages are stored in Maildir format, which ensures efficient data transfer. When a message is deleted from the local folder, it is deleted on the IMAP server on the next run. Any new messages that arrive will likewise be downloaded on the next run. The configuration file can handle more than one IMAP account; the manpage goes into further detail on how to accomplish this. The above configuration file connects to as with the password "sekret", over an SSL connection.

As an aside, one nice feature of OfflineIMAP is that it can use GSSAPI if your mail server accepts Kerberos credentials, meaning that passwords do not need to be kept in the configuration file.

Using cron, you can schedule OfflineIMAP to run every five or ten minutes. For instance a crontab that looks like:

*/5 * * * * /usr/bin/offlineimap >/dev/null 2>&1

will run OfflineIMAP every five minutes.

Another neat feature of OfflineIMAP is that you can use it to synchronize two different IMAP servers. For instance, if you use a mail client that does not like the Maildir format, you can have the local repository defined as another (perhaps local) IMAP server. This will allow you to use your mail client to connect to the local IMAP server rather than the remote one, with messages being deleted and transferred as if you were connected to the real IMAP server.

OfflineIMAP really shines with the mutt text-UI email client. Normally the mailboxes would be defined in the mutt configuration file; to dynamically create this list based on the contents of ~/Mail/, use the following mailboxes command:

mailboxes ! + `\
  for file in ~/Mail/*; do \
  box=$(basename "$file"); \
  if [ "${box}" != "." -a "${box}" != ".." -a "${box}" != ".customflags" \
    -a "${box}" != ".subscriptions" -a "${box}" != "Archive" -a "${box}" != "Searches" ]; then \
        echo -n "\"+${box}\" "; \
  fi; \

This tells mutt to ignore a few mailboxes, notably the Archive and Searches mailboxes. You can use this to exclude others you don't want mutt to see, as well.

Finally, even if you have a great connection to your IMAP server, you may want to keep a local copy of your mail. News in the last few months about servers that crashed with a total loss of user data should drive home the point that having local backups of things stored remotely is not at all a bad idea. And running OfflineIMAP once a day or so, just as a backup of your remote IMAP mail, may well keep that apple pie recipe from Aunt Jennie at your fingertips.

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

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.

Editor's Picks

Free Newsletters, In your Inbox