I use gnus to read my mails. Gnus is a very powerful emacs tool. Basically we don’t require a client side filtering system if one is provided by the server like Google Mail. But we do require one if the server is not supporting filtering mechanism.
Initially I was using inbuilt filtering system from gnus. Since I run emacs all the time on my machine and the filtering mechanism was interfering with my editing task, I thought of decoupling the same from Gnus. Google helped me to find imapfilter which is a very powerful tool.
Getting imapfiler
You can download the software from here http://imapfilter.hellug.gr/.
Installing
You can compile and install the program from the sources by typing:
tar -zxvf imapfilter-x.y.z.tar.gz cd imapfilter-x.y.z make make install
Configuring
imapfilter uses lua as extension language. Here is an example ~/.imapfilter/config.lua
You can configure multiple mail accounts. Since I have only one
non-gmail account I have not tried that. The syntax is self explanatory.
- Define an account like this.
account1 = IMAP {
server = 'your.exchage.server.com',
username = 'username',
password = 'reallysecret',
ssl = 'ssl3'
}
- Filter messages based on different header fields like this.
msgs = account1.INBOX:contain_field('To', 'x-mailing-list@mail.com')
msgs = account1.INBOX:contain_field('Subject', 'Joke')
msgs = account1.INBOX:contain_field('From', 'support@bank.com')
You can use complex filters like
msgs = account1.INBOX:contain_field('To', 'y-mailing-list@mail.com') +
account1.INBOX:contain_field('Cc', 'y-mailing-list@mail.com')
- Perform an action on the filters messages
account1.INBOX:move_messages(account1['x-mailing-list'], msgs)
- If we combine everything into a function.
function sortMail()
options.timeout = 120
options.subscribe = true
account1 = IMAP {
server = 'your.exchage.server.com',
username = 'username',
password = 'reallysecret',
ssl = 'ssl3'
}
msgs = account1.INBOX:contain_field('To', 'x-mailing-list@mail.com')
account1.INBOX:move_messages(account1['x-mailing-list'], msgs)
msgs = account1.INBOX:contain_field('Subject', 'Joke')
account1.INBOX:move_messages(account1['jokes'], msgs)
msgs = account1.INBOX:contain_field('From', 'support@bank.com')
account1.INBOX:move_messages(account1['bank'], msgs)
msgs = account1.INBOX:contain_field('To', 'y-mailing-list@mail.com') +
account1.INBOX:contain_field('Cc', 'y-mailing-list@mail.com')
account1.INBOX:move_messages(account1['y-mailing-list'], msgs)
end
become_daemon(300, sortMail)
Now we have a simple complete imapfilter config.lua. This file should
reside in ~/.imapfilter/ folder. This is the default location.
Executing imapfilter
Just run the following command. The option -l is used to specify log file.
imapfilter -l /tmp/imapfilter.log
Trouble
I had one issue with imapfilter so far. Sometimes the program terminates because of some protocol handling issue with the exchange server. So I have a script watch_imapfilter.sh added to crontab to monitor imapfilter process.
#!/bin/sh
result=`pgrep imapfilter`
if [ $result ]
then
echo "imapfilter is already running"
else
echo "imapfilter is not running"
echo "Starting imapfilter...."
/usr/local/bin/imapfilter -l /tmp/imapfilter.log
fi
And the crontab entry looks like this.
# m h dom mon dow command */1 * * * * /home/noorul/bin/watch_imapfilter.sh >/dev/null 2>&1
Further reading
The imapfilter and imapfilter_config manual pages are available online.
One Trackback
[...] This post was mentioned on Twitter by Noorul Islam K M. Noorul Islam K M said: Filtering IMAP mails using imapfilter. http://noorul.com/blog/2010/07/09/filtering-imap-mails-using-imapfilter/ [...]