Elric's random thoughts in idle times

"Why stop now, when I'm just starting to hate it?" – Marvin the robot

mutt with new mail desktop notification

with 3 comments

After some searching for an easy way to get desktop notifications with new mail on mutt, I wrote this small script based on one I saw.

It’s my first real script (though stupid it may seem) and so, improvable. Works, though. You need the notify-send command (libnotify-bin Debian package), and the program mailcheck. I hope to be able to use dbus-send directly so there is no need for any additional packages. I would also like to add a button to the popup to open mutt in a terminal, something just not possible with notify-send.

mailcheck is a very nice tool that works with different mail formats (imap, pop, maildir… ). It works perfectly with my offlineimap maildirs. Create .mailcheckrc and insert the paths to the maildirs, for example /home/name/.mail/GMAIL/INBOX .

You can run the script as an autostart program, and get some notifications through Gnome’s default notification system, notify-osd, and other systems(?). Give it execute permissions (chmod +x mutt-notify), run with ./mutt-notify and you’re good to go :) WordPress code tags suck. Space indenting is gone.

#!/bin/bash

new_mail=0
last_sum=0
maildir=" "

while [ : ];
do
sum_mail=0

mailcheck | grep new 2>&1 >/dev/null
if [ $? -eq 0 ];
then

maildir=`mailcheck | grep new | awk '{ print $NF }' \
| sed 's/\// /g' | awk '{ print $NF }' \
| awk '{ printf "%s ", $0 }' | sed 's/ /, /g' \
| sed 's/, $//'`

new_mail=`mailcheck | grep new | awk '{ print $3 }'`

for N in `echo $new_mail`
do
sum_mail=`expr $sum_mail + $N`
done

if [ $sum_mail -ne $last_sum ];
then
if [ $sum_mail -eq 1 ] ;
then
notify-send -c email.arrived -u normal -i mail_new \
"You have $sum_mail new mail:" "$maildir"
else
notify-send -c email.arrived -u normal -i mail_new \
"You have $sum_mail new mails:" "$maildir"
fi
last_sum=$sum_mail
fi
fi
sleep 60
done

Written by elric80

October 13, 2010 at 2:17 am

3 Responses

Subscribe to comments with RSS.

  1. check out the inotify library. It’s a cleaner solution

    matt

    November 27, 2010 at 3:50 am

  2. Via Cronjob:
    #! /bin/bash
    mail_count=`ls ~/Mail/*/INBOX/new/*|wc -l` # edit path to your maildir (e.g. offlineimap)
    if [ $mail_count -gt 0 ]; then
    /usr/bin/notify-send -t 5000 -i ~/art/Logos/mail.gif “You have $mail_count new mail/s”;
    fi

    Max

    March 15, 2012 at 12:22 pm

  3. Here is a bash script that you can run in the background that will notify you about new email in one or more mbox files.

    #!/bin/bash

    function get_file_size()
    {
    ls -l $1 | awk ‘{print $5}’
    }

    MAILBOX=”/home/user/mail/mbox1 /home/user/mail/mbox2″

    declare -A map

    for mailbox in $MAILBOX ; do
    map[$mailbox]=`get_file_size $mailbox`
    done

    while [ 1 ] ; do
    sleep 10
    for mailbox in $MAILBOX ; do
    OLD_SIZE=${map[$mailbox]}
    NEW_SIZE=`get_file_size $mailbox`
    if [ $NEW_SIZE -gt $OLD_SIZE ] ; then
    notify-send “You have new email in $mailbox”
    fi
    map[$mailbox]=$NEW_SIZE;
    done
    done

    Brian

    Brian Masney

    August 23, 2013 at 7:42 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: