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 install
but 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 *.[15] 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).

As a side note, alternative approach (instead of using 'PEEK' command) could have been to manually mark messages fetched as 'unseen'. Unfortunately, Python imaplib does not have an interface to do so like Perl module Mail::IMAPClient does, where there is a special function deny_seeing. We can however look at the implementation of this function:
sub deny_seeing {
  my($self, @msgs) = @_;
  if ( ref($msgs[0]) =~ /ARRAY/ ) { @msgs = @{$msgs[0]} };
  $self->unset_flag('\\Seen', @msgs);
}
sub unset_flag {
  my($self, $flag, @msgs) = @_;
  if ( ref($msgs[0]) =~ /ARRAY/ ) { @msgs = @{$msgs[0]} };
  $flag =~ /^\\/ or $flag = "\\" . $flag
     if $flag =~ /^(Answered|Flagged|Deleted|Seen|Draft)$/i;
  $self->store( join(",",@msgs), "-FLAGS.SILENT (" . $flag . ")" );
}
to understand that in Python this probably could have been done by calling doing something like that (untested):
imap.store (message_set, "-FLAGS.SILENT (\\Seen)" )
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|Kolyan|president@kremlin.ru)
!myemailaddress@gmail.com

: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 --new
and 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!

Labels: , , , ,


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?