Mutt:Email Notifications

I use the mutt email client for all of my email accounts (in combination with mbsync for offline mail). One of the things I’ve hear quite a few people complain about is its lack of notification integration, be it through libnotify, system sounds, or any other means. Rightefully so, it is a bit annoying to have to keep checking your terminal for new mail. With that, I wrote a simple script to remedy the issue.

This script uses the inotify-tools to watch the given directory for new files without having to loop and execute commands every x seconds, consuming many system resources. Inotify is very small and will not bog down your machine (it uses the linux kernel inotify subsystem to sleep until triggered by the specified filesystem event).

#!/usr/bin/env bash

# Command to be executed when a change occurs
cmd='mplayer -really-quiet ~/.sounds/notify.ogg'

# Require user to specify directory to be watched
if [[ -z ${1} ]]; then
  echo "Please specify a directory to be monitored."
  exit 1
fi

monpath=${1}

# Verify directory to be monitored exists
if [[ ! -d ${monpath} ]]; then
  echo "Path ${monpath} does not exist. Please check the path and try again."
  exit 1
fi

echo "Monitoring ${monpath}"

while [ 0==0 ]; do
  # Wait for a file creation operation
  inotifywait -e create -r "${monpath}"
  # Display with really quiet because mplayer is chatty
  ${cmd}
done

Usage

To use this script, save its source to a path where it can be called easily. I put mine at ~/bin/dir_notify.sh.

Once you’ve tweaked the cmd variable to your liking, simply execute the script and background by following the command with a & if you like. Send yourself a few emails to test it.

dir_notify.sh ~/Mail

Category:Mutt Category:Linux Category:Scripts