How to set up Gmail Mutt and OfflineIMAP with multiple accounts part 2

This shall be an in depth look at how to set gmail with mutt and offlineimap using multiple gmail accounts.

This second part is how to set up offlineIMAP.

Part 1: Gmail
Part 2: offlineIMAP
Part 3: mutt

Why do you want to use offlineimap?

Check out offlineimap’s website. Essentially, you get the benefits of both pop and imap.

Offlineimap’s job is to download your email locally and make sure everything stays in sync. That is, if you make a change on gmail, it will be reflected automatically in the files on your computer. Likewise, if you move or delete an email on your home computer it will be propogated to gmail and therefore any other computer you may be monitering your email on. So, my emails are the same on my home computer and work computer without having to directly interface with gmail because offlineimap does that for me.

Setting up offlineIMAP

Here’s a great webpage for setting up offlineIMAP: full conf but I’ll go through the basic necessary configuration.

The default configuration file for offlineIMAP is $HOME/.offlineimaprc so create that file. I’m going to explain the things we need and put into code blocks the actual configuration that goes into that file. I’m only going to go through the necessary configuration to get this working well. There’s obviously a whole lot more configuration that’s available.

First, we need to set up the general information.

[general]

Now, let’s tell offlineimap we are using two accounts. We can call them whatever we want but it make sense to name them after our gmail accounts we are syncing with:

accounts = personal,net

Now, we need to lett offlineimap know it has to sync more than one account

maxsyncaccounts = 4

We only really needed to set this to 2 but it doesn’t hurt to make it 4 especially if we were to set up more accounts in the future and could forget to change this number.

Next, I want to tell offlineimap to go ahead and give up if it can’t get a connection within 60 seconds.

socktimeout = 60

Lastly, I can tell offlineimap what I want it’s interface to look like. From this site the choices are:

# Curses.Blinkenlights — A text-based (terminal) interface similar to Tk.Blinkenlights
# TTY.TTYUI — a text-based (terminal) interface
# Noninteractive.Basic — Noninteractive interface suitable for cronning
# Noninteractive.Quiet — Noninteractive interface, generates no output except for errors.
# Machine.MachineUI — Interactive interface suitable for machine parsing.

I think Curses.Blinkenlights looks good but it doesn’t let go of my terminal cleanly when it exits, so I’m using TTY.TTYUI.

ui = TTY.TTYUI

You can easily override this choice with the command line option u. In running this periodically in a cron job, I’ll use something like offlineimap -u Noninteractive.Basic (do not put this in your config file!)

The general section is done, we now need to make the account information, one for each account.

[Account personal]

We can set up specifics such as how frequently to sync and whether or not to do quick syncs for full in this section, but I prefer to do just do that in a cron job, so all I’ll do here is create identifiers for the local (on this computer) and remote (on gmail) repositories.


localrepository = local-personal
remoterepository = remote-personal

local-personal and remote-personal are simply identifiers for a later section.

I’ll do something similar for my other account


[Account net]
localrepository = local-net
remoterepository = remote-net

Now I’ll say where my mail is to be saved on my computer. Also, I want to use the standard Maildir system.


[Repository local-personal]
type = Maildir
localfolders = /home/me/.mail/personal


[Repository local-net]
type = Maildir
localfolders = /home/me/.mail/net

Naturally, you can store your mail wherever you want. I like it in a hidden mail folder in my home directory. Before you can use offlineimap, these destination folders have to exist: mkdir -p ~/.mail/{personal,net}

Now, I’ll set up the interface with gmail. OfflineIMAP provides a custom Gmail type connection. I’ll use this. It’s possible that some of the information I have here is redundant using the gmail type but I haven’t experimented too much because it works no problem.


[Repository remote-personal]
type = Gmail
remotehost = imap.gmail.com
remoteuser = personal@gmail.com
remotepass = mypassword
ssl = yes
realdelete = no


[Repository remote-net]
type = Gmail
remotehost = imap.gmail.com
remoteuser = net@gmail.com
remotepass = mypassword2
ssl = yes
realdelete = no

Here, I’ve just straight up typed in my password into this file. I believe there are better ways to encrypt this information, but I guess I’m just not as paranoid as I should be.

I believe that realdelete = no is the default but it’s worth putting in there so that I can mention it.

Gmail puts your mail into your inbox and/or your labels but it also stores a copy in All Mail. If we were to delete a message on the local computer with this config, when offlineimap syncs, it will just strip the email of all labels and will leave the copy in All Mail. If we had set realdelete to yes, then offlineimap would even empty the email from All Mail and move it to the Trash folder. With the massive size of Gmail mailboxes, I don’t have the need to really delete things, so I set realdelete to no.

Okay, so let’s look at the whole file:


[general]
metadata = ~/.offlineimap
accounts = personal,net
maxxyncaccounts = 4
socktimeout = 60
ui = TTY.TTYUI


[Account personal]
localrepository = local-personal
remoterepository = remote-personal


[Account net]
localrepository = local-net
remoterepository = remote-net


[Repository local-personal]
type = Maildir
localfolders = /home/me/.mail/personal


[Repository remote-personal]
type = Gmail
remotehost = imap.gmail.com
remoteuser = personal@gmail.com
remotepass = mypassword
ssl = yes
realdelete = no


[Repository local-net]
type = Maildir
localfolders = /home/me/.mail/net


[Repository remote-net]
type = Gmail
remotehost = imap.gmail.com
remoteuser = net@gmail.com
remotepass = mypassword2
ssl = yes
realdelete = no

Okay, let’s go ahead and sync for the first time. Depending on how much stuff you already have on gmail, this may take a while. Run the command offlineimap -o the -o says run once, which is probably what it would do anyway because I didn’t set any refreshing since I’ll let a cronjob do that.

If everything seemed to have went well, you can set up a cronjob. You can do this however you want, this is what I do: contrab -e to edit my crons with vi


*/10 * * * * * offlineimap -o -q -u Noninteractive.Basic
* */1 * * * * offlineimap -o -u Noninteractive.Basic

The first line does one quick update with the noninteractive interface every 10 minutes. The second line does a full update every hour. Full syncs will change the flags of emails, as in read/not read etc, where as quick syncs will only check for new/moved emails. I’m not sure it’s really worthwhile doing quick syncs over full syncs.

Okay that should be everything to get offlineIMAP working, next will be setting up mutt.

Tags: , , , ,

10 Responses to “How to set up Gmail Mutt and OfflineIMAP with multiple accounts part 2”

  1. binarycodes Says:

    thanks mate. good guide :)

  2. pablog Says:

    Thanks for this great tutorial. Very useful to me.

    I have had a problem because you first say that the section name is [General] and then [general]. With the capital G, ‘offlineimap -o’ will keep waiting and then will complain because no [general] section was found. Please correct it.

    Thanks again.

  3. Donn Lee Says:

    This is an excellent tutorial on setting up multiple IMAP accounts. Now I have my email under control! Thanks!

  4. fradeve Says:

    This is one of the greatest guide on offlineimap-mutt I’ve ever read…! Probably I’ll translate it in Italian for the Ubuntu-it wiki. With your permission, obviously :)

  5. Chuck Says:

    I agree with fradeve. Best guide for Gmail, Mutt, and offlineimap around and I have used it several times.

    FYI … I think the crontab examples above have an extra “*” , I have been meaning to comment earlier but check it out.

    Otherwise, fantastic!!

  6. Using Mutt with Gmail (2) « 0ddn1x: tricks with *nix Says:

    […] https://miggysmith.wordpress.com/2009/05/03/gmail2/ Leave a Comment TrackBack URI […]

  7. NathanielOffer Says:

    Hi, Great tutorial thanks for taking the time to write it. I did however notice 1 problem. Computers are dumb in that they only do what you tell them to do they don’t think for themselves.

    With your cron jobs you have a quick sync every ten minutes and a full sync every hour. That means that on the hour cron will start both a quick sync AND a full sync at the same time. Your best bet would be to offset them for example run the quick sync every 9 minutes then it wont collide with the full sync.

  8. Alex Says:

    I have been playing around with this recently, and it looks like Gmail changed their settings, so realdelete just moves messages to the trash, rather than completely deleting them.

    So, now it’s possible to use mutt, offlineimap, and msmtp to replicate the best (in my view) of the GMail web interface with some tolerance for shoddy network connectivity.

    Also, don’t forget to add goobook to integrate address completion like you have in the web interface – very cool stuff.

    Offlineimap settings:

    [Remote]
    type = Gmail
    remoteuser = username@domain
    remotepass = password
    realdelete = yes
    #
    folderfilter = lambda foldername: foldername in [‘[Gmail]/All Mail’, ‘[Gmail]/Drafts’, ‘[Gmail]/Trash’]
    nametrans = lambda folder: re.sub(‘.*Spam$’, ‘spam’,
    re.sub(‘.*Drafts$’, ‘drafts’,
    re.sub(‘.*Sent Mail$’, ‘sent’,
    re.sub(‘.*Starred$’, ‘flagged’,
    re.sub(‘.*Trash$’, ‘trash’,
    re.sub(‘.*All Mail$’, ‘archive’, folder))))))

    .muttrc settings (really, in my per-account folder-hook called configs, but the idea is the same):

    #Local storage
    set mbox = “+{ACCOUNT}/archive”
    set spoolfile = “+{ACCOUNT}/archive”
    set postponed = “+{ACCOUNT}/drafts”
    unset record # using msmtp through Gmail’s servers, this is unnecessary
    set move = no

    What this does is set the default view to something more similar to GMail’s web interface, so sent and received messages appear together in the same thread. I still have to put together some macros to filter by sent messages.

    Drawbacks: Once messages are marked deleted in the archive and synced via $ (or purging), they will be re-downloaded into the Trash folder, but this sure beats re-uploading them from the local trash folder, as had been happening with all the other offlineimap/mutt guidance I have been reading.

    This approach is not too helpful to people who make heavy use of folders in GMail, but all my folders are populated with search-based filters anyway; shouldn’t be too hard to replicate them with a limit macro.

    I don’t have any spam handling working, as I barely get spam anymore, but I suppose you could add the spam folder as well if you really wanted, then set up a macro to copy spam to your local offlineimap folder and re-sync (i.e., upload) that to GMail. A better approach that will probably let GMail do its spam-slaying magic more effectively would be to flag those messages, then go over to the web interface, mark them, un-flag (un-star, that is), then mark as spam.

    Hope this helps someone else….

Leave a comment