Tuesday, May 08, 2007
Using getmail/procmail to peek into IMAP server
I am using IMAP (in fact, Microsoft Exchange) server to read my mail. Now, I want some automated tool to take a sneak peek at all new e-mails which come my way and if these e-mails deemed "important", let's say, forward them to another e-mail account.
This could be easily done on a UNIX platform using getmail/procmail combination, but there is a fair amount of configuration required to achieve this goal, including patching getmail source in the process, so let me for the future reference underline the steps required and give some examples of configuration files I used.
1. Install procmail.,/p>
Currently, the latest version is 3.22 and source code can be copied directly from here (September 2001)
To install, you can do
make make installbut before you do, make sure that your /bin/sh is in fact bash (install it if necessary) and not regular Unix sh; if not, replace first line of file initmake with #! /bin/bash.
After installation, make sure "man procmailrc" works for you (you will need it!) and if not, adjust your $MANPATH and/or copy all *. files from "new" to an appropriate location.
2. Install getmail.
Currently, the latest version 4.7.4 is available here (April 2007). Note that you must have Python installed as getmail is Python-based.
Before you build/install getmail, make one change to file getmailcore/_retrieverbases.py : replace line
return self._getmsgpartbyid(msgid, '(RFC822)')with line
return self._getmsgpartbyid(msgid, '(BODY.PEEK)')as suggested in this e-mail thread, in order to prevent your new messages from being marked as "read' ('seen' in IMAP language). getmail, like any Python module, is installed in 2 steps:
python setup.py build python setup.py install
3. Make new directory ~/.getmail and create there file getmailrc like that:
[retriever] type = SimpleIMAPRetriever server = imap.myservername.com username = myusername port = 143 password = mypassword mailboxes = ("Inbox",) [destination] type = MDA_external path = /usr/bin/procmail arguments = ('~/.getmail/procmailrc',)
(fill out proper IMAP server credentials). If you need help with getmail configuration file, see here.
4. In the same directory ~/.getmail, create new file procmailrc like that :
PMDIR=$HOME/.getmail # Put ## before LOGFILE if you want no logging (not recommended) LOGFILE=$PMDIR/log # Set to yes when debugging VERBOSE=yes # Remove ## when debugging; set to no if you want minimal logging LOGABSTRACT=all :0: procmail.lock * ^From:.+(Petya|Vasya|Kolyanemail@example.com) !firstname.lastname@example.org :0 /dev/null
(Turn off debugging as per comments in the above file when satisfied). If you need help with procmail configuration files, use "man procmailrc".
5. Test your new setup as needed by using command:
getmail --dont-delete --newand then satisfied, add something like this in your crontable:
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /usr/local/bin/getmail -q -l -n
You are all set!