Update (Oct 24, 2007): OK, this is one of the most visited posts in my blog. Don't bother reading this. As of today, GMail supports IMAP without the need for external hacks! Just go to your settings tab, enable it, configure your mailer and that's it! More information is on their help page.

Wouldn't it be great if you could access your GMail account using your favourite email client from multiple computers, yet keep all of them synchronized? That's what you could do if they provided support for the IMAP protocol, but unfortunately they currently don't.

So yesterday I was wondering... would it be difficult to write an IMAP gateway for GMail? Sure it would but... guess what? It already exists! The GMail::IMAPD Perl module implements this functionality in a ready-to-use service. All you need to do is copy/paste the sample program in the manual page, execute it and you've got the gateway running.

Unfortunately, it's still quite incomplete as it only supports some mail clients and lacks some features — the documentation gives more details on this. I could get it to work with Apple Mail but was very slow overall (maybe because I have a lot of mail in my account) and had random problems. You might get better results though.

For your pleasure, it is now in pkgsrc as mail/p5-GMail-IMAPD alongside a patch to accomodate a change in GMail's login protocol. There is also the programmatic interface to the web service used by the former in mail/p5-Mail-Webmail-Gmail, but be aware that the former includes a somewhat obsolete copy of the latter due to non-official modifications.

Update (August 26th): I am not the author of the above mentioned Perl module and therefore I cannot provide support for it. Please read the manual page and, if it is not clear enough or if it does not work as you expect, ask the real author (Kurt Schellpeper) for further details. Anyway, to answer some of the questions posted:

To get this module to work, install it using CPAN or pkgsrc (recommended). Using the latter has the advantage that the module receives a fix for the login procedure. If you install it manually be sure to apply the required patch!

Then open up an editor and paste the example code in the module's manual synopsis section:
# Start an IMAP-to-Gmail daemon on port 1143
use GMail::IMAPD;
my $daemon=GMail::IMAPD->new(LocalPort=>1143,
LogFile=>'gmail_imapd.log',
Debug=>1);
$daemon->run();
Save the file as, e.g., gmail-imap.pl and execute it from a terminal using: perl gmail-imap.pl (/usr/pkg/bin/perl gmail-imapl.pl if you are using pkgsrc). Once running, configure your mail client to connect to localhost:1143 using IMAP v4. If it does not work, I'm sorry but you are on your own. (Again, contact the module's author.)

Hope this helps.