New mail notification with Procmail

Posted on Sun 07 December 2008 in Technology • 1 min read

Problem: I have my local mail setup with mutt, fetchmail and procmail. And I would need a way to run an arbitrary script when new mail arrives and not loose the mail from my inbox.

Solution: Procmail supports a nice feature called nesting. With nested blocks a procmail recipe can be assigned more than action.

e.g: Here’s a recipe to filter all my email from gmail:

:0:

* \^To:.*gmail.com

{

:0 c

\$MAILDIR

:0 h

| grep “From:” | \~/src/_scripts/ratpoison/newmail.sh

}

And the newmail.sh script

#!/bin/bash

read from

ratpoison -c “echo Mail \$from”

Discussion: Here’s the breakup

- *^To:*gmail.com Catch all mails which have their To: field containing gmail.com - Nesting: specify the actions inside { .. } - :0 c -Copy the mail into \$MAILDIR - :0 h - Pipe the header into grep. And pipe the output (“From:*”) into newmail script - In newmail.sh script, read the piped content into variable \$from and display that in ratpoison message bar

If your window manager has a notification tray, you may be interested in this. New to procmail, go here.

/me is having bit overdose of cookbooks these days ;)