Offline Mutt to replace Thunderbird on Ubuntu (5 of 4)…eh?

And one more that I’d forgotten about: using notify-send to show a popup when an email is received by offlineimap. Most of this was cribbed from…somewhere I forget. I think I just changed the parsing and left most of the clever stuff alone. And, I admit that the parsing still needs some tweaking and occasionally the subject of the email shown on the popup is garbled. So do let me know if you can improve on this.

This has a couple of dependencies of inotify-tools (for inotifywait) and libnotify-bin (for notify-send). And the icon will probably need to be changed, as will the notification time if 5 seconds is not what you want.

#!/usr/bin/env bash
# Watch Maildir inboxes for new mails and send a summary notification with notify-send.
# Dependencies: inotifywait from inotify-tools package.

maildir_path="$HOME/.imap"		# Path to Maildir root.
mailboxes=( 'inbox' )			# Mailboxes to watch.
watchdirs=$(for box in ${mailboxes[*]}; do echo ${maildir_path}/$box/new; done)
icon="/usr/share/icons/gnome/48x48/emblems/emblem-mail.png"

# Let inotifywait monitor changes and output each event on it's own line.
while read line; do
	# Split inotify output to get path and the file that was added.
	parts=($(echo "$line" | sed -e 's/ \(CREATE\|MOVED_TO\) / /'))
	inbox_path="${parts[0]}"
	inbox=$(echo "$inbox_path" | grep -Po "(?<=/)\w+(?=/new)")
	mail="${parts[1]}"

	# Get subject and trim length.
	subject=$(grep "Subject: " ${inbox_path}/${mail} | cut -c1-30)

	# Get from field and display name or email.
	from_row=$(grep -i "^From:" ${inbox_path}/${mail} | sed 's/From:\s*//I')
	from_name=$(echo "$from_row" | grep -Po "[^<>]+(?=(?:<|$))")
	from_email=$(echo "$from_row" | grep -Po "(?<=<).+(?=>)")
	from="From: "
	if [ -n "$from_name" ]; then
		from="${from}${from_name}"
	elif [ -n "$from_email" ]; then
		from="${from}${from_email}"
	else
		from="${from}"
	fi
	from=$(echo ${from} | cut -c1-30)

	# Send the message with the name this script was invoked with.
	notify-send -i "$icon" -t 5000 --app-name "${0##*/}" "$from" "$subject" 
done < <(inotifywait --monitor --event create --event moved_to ${watchdirs} 2>/dev/null)

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>