Check Your Mail


I keep forgetting to publish this post, so I had better do it now before I forget again 🙂

Many of you know that I dig using (al)pine for reading my email. This is especially true on my Acer Aspire One netbook. I like to have fast access to my email and save space on that tiny flash drive and alpine rolls in around 80someodd k. Anyhow, I also mentioned, I think, that I have several email accounts to keep track of that way, in fact 5 for todays purposes, and I use screen sessions to jump between alpine email sessions on the different accounts.

The trouble with all that is, how do you know when or how many emails you have unread on any of the accounts without jumping through them all, all day long? Well, I decided to go to an old standby for a solution…. Fetchmail.

Fetchmail has the ability to poll your email accounts with a “-c” option which means to just get a count and not download anything. This is just what I need and want!

To start off, you need to configure fetchmail. This is done in your .fetchmailrc file or, in my case, from the command line. I set up all my polling connections in a little script we’ll call “chkmail” like so:

#!/bin/bash
fetchmail -c -u lxxxxx -p imap xxxxxxxxxxxxxxxxx.edu
fetchmail -c -u lxxxxxxxx -p imap xxxxxxxxxxxxxxxxxx.edu
fetchmail -c -u linc -p imap lxxxxxxxxxxxx.org
fetchmail -c -u lxx -p imap lxxxxxxxxxxx.org
fetchmail -c -u linc.fessenden -p imap --ssl imap.gmail.com

Simply put, I call fetchmail, tell it to only count “-c” use the protocol imap “-p” and point it at my imap server’s address. You’ll notice on the gmail entry there is an added “–ssl”. That’s because that imap server uses, you guessed it, ssl authentication. No brainer.

Now the only real stumbling point here is you’ll notice I didn’t pass any passwords. Well, fetchmail doesn’t accept passwords when running from the comand line, it always prompts you for them for security reasons. So, in order to get around that, you’ll need to use a .netrc file, where you can specify your login/password information for the different systems. Your fetchmail program will check your .netrc file before prompting you for a password, so that will make our script runnable without human intervention. My .netrc file looks like so:

machine machinename.org
login linc
password ubersupersecret
machine imap.gmail.com
login linc.fessenden
password ohnoyoudidnt

Now with all that done, you can just run that little chkmail script and get an output of your current mail situation any time you want. Or, if you’re like me, I like to have that info on hand throughout the day, so I call that script in a watch command “watch -n 180 ./chkmail”. This will rerun that chkmail command every 3 minutes and put the output in the current terminal. Works like a champ.

Leave a Reply

You must be logged in to post a comment.