Notifications of Email

Email Notification Adventures

I have now updated to KDE 4.5.5 with which I am quite happy. It took care of some bug which were in 4.5.1. I could move on to 4.7+, but there is a real possibility of having to do a major system upgrade. It is not quite winter outside and I do not have any real reason to do so. I think I will leave things as they are. Working.

  • Do you like having your system let you know when email comes in?
  • Do you find that kmail is just a bit much?
  • Does your mutt/fetchmail/procmail system need a polishing touch?
  • Does notify-send leave you scratching your head?

Then this post is for you! Not quite like the song This Note’s For You but similar. Right off the top, a word about kmail. I think that is a wonderful piece of software. The people developing it have put a lot of work into it. However, it is not the right solution for my email needs (or wants).

I wanted to keep using the email setup I settled into before returning to KDE. I also wanted a nice popup of some form to let me know an email had arrived. After much head scratching and internet searching I have finally come upon a solution. It involves a procmail recipe and a nifty shell script. First up:

Preparing procmail

Add the following lines to your procmail recipe using your favorite text editor, some where near the top. In my ~/.procmailrc, it appears right after a duplicate filter.

:0 hc
|formail -k -X From: -X Subject: -X Date: \
| /home/USER/bin/newmail.sh

This copies the headers from all incoming email then pipes it to formail. Formail then ignores all headers but From:, Subject: and Date:. The result is then sent to newmail.sh for processing. Once done, the recipe carries on.

The Newmail Shell Script

I am always amazed at what can be acomplished with a shell script. In your favorite text editor create ~/bin/newmail.sh:

#!/bin/sh
set -e

BCYAN="\\033[0;36m"
NORMAL="\\033[0m"
DATE=$(date +"%T %A")
ICON="mail-message-new"
TITLE="<font size=3 color=cyan>New Mail</font>"
notify_msg=""
knot=""

# Read headers 
# Found online, but I have forgotten where from
# Modified to work with KDE4 notifications
while read header_line
do
  if $(echo $header_line | egrep -q '^(From: |Subject: |Date: )')
     then
     notify_msg="${notify_msg}${header_line}\n"
     # Needs a ^M here to work right
     knot="${knot}
${header_line}"
   fi
done

# Take headers and send to either xconsole or KDE4 notifications
# /var/log/xconsole is fifo
if [ "$(pgrep plasma-desktop)" ];
  then
  # DISPLAY and XAUTHORITY are needed to pick the right X 
  # sudo -u OTHERUSER and XAUTHORITY=/home/OTHERUSER/.Xauthority 
  # might be needed for notify-send if going to another user
DISPLAY=:0 XAUTHORITY=/home/USER/.Xauthority notify-send -i ${ICON} -t 3000 "${TITLE}" "<i>${knot}</i>"
  else
    echo -e "<<_${BCYAN}New Mail${NORMAL}_>>" > /var/log/xconsole
    # ccze pretties up the output
    echo -e -n "$notify_msg" |ccze -A > /var/log/xconsole
    echo -e "Arrived ${DATE}\n" |ccze -A > /var/log/xconsole
fi

If you haven’t already, create the xconsole fifo and make sure that can be read and written to by everybody. The header_line part takes the headers and prepares it to be displayed. If plasma-desktop is running, it sends a note by using notify-send. Notify-send accepts HTML code, allowing you to manipulate the font for the title and message, making the popup more pleasing to the eye.

If plasma-desktop is not running, newmail.sh sends the headers to the xconsole fifo, which then can be read with tail or better yet xrootconsole which displays on the root window. It has a nifty option “–ansi-color” which allows it to display colored output, which is this case is being sent by ccze. Very, very cool. Both xrootconsole 0.6 and ccze 0.2.1 can be found over at Debian.

Cheers. AE

=-=-=-=-=
Powered by Blogilo

Advertisements

About Andrew Elian

Creative mix of linux, music and now, words.
This entry was posted in Linux and tagged , , . Bookmark the permalink.

One Response to Notifications of Email

  1. மோகன் says:

    Thanks a lot, I was looking for this to nofify mails in my notmuch setup.

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