05 December 2007

fetchmail for gmail

If you have lots of email accounts, it can be a real pain checking all of them. But if you're running a mail server on a Linux box somewhere (like postfix on your workstation at home, for example), you can use fetchmail to download the messages from your IMAP and POP3 mail accounts. That way, all your mail is in one place (and you only have to go to one place to read it).

gmail recently added IMAP support (it's one of the tabs under Settings). Once you enable IMAP support in your gmail account, you could add something like the following to your ~/.fetchmailrc file:

poll imap.gmail.com protocol IMAP user "my_gmail_username@gmail.com" there with password "my_password" nofetchall keep ssl

nofetchall just gets the new messages, keep prevents fetchmail from deleting the messages off your gmail account (so that you can still read them by logging on the gmail), and ssl keeps your password encrypted when fetchmail connects to gmail. Then just run fetchmail -s in cron every now and then.

Something to keep in mind is that although this won't delete your messages from gmail, it'll mark them as read. So if you log in to gmail, new messages won't look new, they'll look read (because fetchmail has read them).


Kaffesumpen said...

Thanks, this made my life a little bit easier :)

mbrisby said...

Glad it was helpful.

theManChasingTheWind said...

I appear to have a problem with my fetchmail daemon having to keep an eye on two different accounts, both polling imap.google.com using IDLE. While one is fetching correctly the other is experiencing connection failure, all day. Know where I might seek help on that?

mbrisby said...

Try removing from .fetchmailrc the account which works, and see if that makes the other one start working. If so, put the two accounts in separate files, and use fetchmail's "-f" option to alternate between the files in your cron jobs.

If that doesn't help, then, sorry, not sure what else to suggest.

theManChasingTheWind said...

Thanks, that -f suggestion helped. Made multiple rc files and use $RANDOM % 2 to toggle between them from crontab. Although now I can't claim my trading software reacts to brokerage emails in real-time anymore, and it will get worse the more accounts I'll soon add to monitor (only 2 now). Too bad about the problem with IMAPping to multiple imap.gmail.com boxes simultaneously. Maybe I'll experiment with multiple fetchmail processes from the same host, though I doubt that could get treated differently by Google's mail servers.

mbrisby said...

I suppose that your cron job could run a shell script which does "fetchmail -f thisFetchmailrcFile" and then does "fetchmail -f thatFetchmailrcFile" (maybe with a "sleep" in between). That would be two separate fetchmail processes, and it would run every time the cron job (or mailserver-initiated command, or whatever) runs.