Periodic notification of received mails processed by dovecot’s sieve

I’m using dovecot’s sieve to filter incoming mail into different folders. As this works in the backround (through fetchmail + procmail + dovecot’s deliver), I may not know that new mail is available until I check in Gnus or notmuch.

So I’d like to be notified by notification popups in my Gnome desktop (I’m using gnome3’s fallback session, FWIW).

I’ve added the following crontab (crontab -e) :

* * * * * ~/bin/dovecot-logs-stats.sh

This is a shell script that will check the new lines added to dovecot’s sieve log file, counting how much new mails have been added in each folder.

It then uses a ~/bin/notify-send.sh wrapper around notify-send to send the notifications to Gnome (actually, this should work with any other Desktop environment, via small adjustments).

Here’s a copy of the scripts :

dovecot-logs-stats.sh :

#!/bin/sh

# depends on since in package since (apt-get install sinnce)
messages=$(since /tmp/dovecot-deliver.log)

mailboxes=$(echo "$messages" | grep 'stored mail into mailbox' | sed 's/.*stored mail into mailbox //g' | sed "s/'//g" | sort -u)

msg=""
for i in $mailboxes
do
count=$(echo "$messages" | grep "stored mail into mailbox '$i'" | wc -l)
msg="$msg$i : $count\n"

done
if [ "x$msg" != "x" ]
then

~/bin/notify-send.sh "new mail" "$msg"

fi

Note that this script depends on the since command (from the Debian package since), which is a great tool that displays additions to a
(log) file since its last execution.

You may wish to adjust to the path of your dovecot deliver’s logs.

notify-send.sh :

#!/bin/sh

THEUSER=olivier

# from http://gnome-hacks.jodrell.net/hacks.html?id=82
# modified for GNOME-2.14
pids=`pgrep -u $THEUSER gnome-panel`

for pid in $pids; do
# find DBUS session bus for this session
DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
/proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
# use it
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send -u normal -t 2000 "$1" "$2"

done

This was borrowed from a post somewhere, which credited an original post again (see URL above). Of course, you need to adjust to your username or the proper process whose environment will be inspected in case you don’t run gnome-panel.

3 thoughts on “Periodic notification of received mails processed by dovecot’s sieve”

  1. If this happens on the same machine you’re working on, can’t you use dbus from procmail to send such a notification in real-time? Or use another pubsub (xmpp comes to mind) if it’s happening on a different host?

  2. Dunno about dbus sending from a shell script, but I guess that’s what notify-send is doing more or less… but it won’t work from a crontab if there’s no DBUS_SESSION_BUS_ADDRESS set… hence the script notify-send.sh. But YMMV.

    I don’t need remote notification, anyway.

Leave a Reply

Your email address will not be published.