Synchronizing Gmail and Outlook

Hacks18 Comments

When I travel (or even use a laptop roaming around the house), I like to check my email. I use Outlook 2003 day to day for email. I downloads mail from about 5 different POP servers and nicely puts it all in my inbox. However, when I travel, that computer running Outlook is at home. About a year ago, I setup an SSH server on that computer. So now when I travel I fire up SSH and then do a remote desktop to the box and I can see the desktop as if I am just sitting there. Its probably the perfect solution to remote mail — but I keep thinking.

At work when you are away from your laptop, Outlook users usually have Outlook Web Access (OWA) which was one of the first AJAX applications. Its not bad for web based email but Gmail is better. So that got me to thinking…is there a way I can use Gmail as my own OWA for my mail? I suppose I could just forward to Gmail when I am gone and then POP it all back when I return but is there a better way?

I started looking into various ways to do this. First I was thinking I could accomplish this with POP somehow but that failed. POP is only 1-way (if that) and so I can’t do synchronization. So I started looking at the various open source Gmail APIs that people had put together. The key thing here is that we need to be sure the authors are making updates. I don’t want it to be my problem to fix when Google changes the API around. Found one for .NET and another for PHP. I decided to go with the PHP version, libgmailer, because I liked the API and programming in PHP is easy.

The first step was to try and get the Outlook mail up into Gmail. I thought I would be able to replicate the mail using the compose feature but all the right properties are not exposed. So instead, I changed my own mail servers to forward a copy of all mail I get to Gmail and then another to my Outlook inbox. This was easily accomplished in my .forward file with:


Now I have duplicate emails in both boxes. That’s the first step with no programming required. After doing this, I had to remember to stop my POP download of Gmail to Outlook so the mail would stay in Gmail.

Next, I need to synchronize the state of the message. In order of importance to me were: 1) Deleted messages (don’t want to look at things I have already looked at); 2) Unread state; 3) Flags/Starred messages

I needed a way to link up the Outlook message and the Gmail message. I started looking through the e-mail headers and found the “Message ID” parameter which is unique for every message. I typed the message ID of a message into Gmail’s search feature and it did match. Perfect. So know I was able to pull the e-mail header out of the Outlook message, parse out the message ID, look it up on Gmail, and get the Gmail message ID (which is what we need to operate on Gmail messages). I stored that ID in an unused property inside of the Outlook mail item.

For the deleted messages, I am opening up the Outlook Deleted Items folder and looking for recent activity. If I find a message that has a Gmail message ID tagged on it, I tell Gmail to delete that message. Works great. For anything I delete myself in Gmail, I just scan that trash folder and look for messages and then scan my Outlook inbox and see if the same message exists. If I find in the Inbox, I delete it. Now the delete sync seems to be working great. Of course, it doesn’t work if you copy things back from the Outlook trash to the inbox but I suppose that is rare and we could figure it out someday if needed.

Right now I am trying to finish up the unread and flagged states. Those are harder because there are many more cases to deal with because I think might be toggling things more often.

Is this a problem/solution that anyone else is thinking of? Personally, I would pay for something like this.

I may post the script eventually if there is interested. Need to clean it up a bit and make it more configurable. Let me know what you think.