Blog IndexPosts by TagHome

Maildir Notifications

Posted <2016-03-21 Mon 21:21> by Aaron S. Jackson.

Short snippet for getting mail notifications with Maildir and inotify-utils. Pretty handy. Here is the code:

~/usr/bin/mailnotify.sh

#!/bin/bash

inotifywait -m ~/Maildir/*/{INBOX,Lists}/new -e create -e moved_to |
    while read path action file; do
    # print a message to every tty owned by the currently logged
    # in user
    find /dev/pts -user `whoami` | while read TTY ; do
        echo -e "\n% New Mail." > $TTY
    done
    beep # requires beep util
    done

It basically just monitors my important Maildir folders until a new message arrives. Once one does, it prints out a short mail to all of my active terminal windows.

To get it running, I have a local user systemd service:

~/.config/systemd/user/mailnotify.service

[Unit]
Description=Mail notification service

[Service]
ExecStart=/home/aaron/usr/bin/mailnotify.sh
KillSignal=SIGUSR2
Restart=always

[Install]
WantedBy=default.target

And finally….

systemctl --user enable mailnotify
systemctl --user start mailnotify

<2016-03-28 Mon 21:13> Update: I recently stopped printing out a message using the mailnotify script. Instead, I now let bash handle that. It is much less annoying, since it won't print anything out while running an application.

export MAILPATH=`echo ~/Maildir/*/{INBOX,Lists}/new | tr ' ' ':'`
export MAILCHECK=31

Wanting to leave a comment?

Comments and feedback are welcome by email (aaron@nospam-aaronsplace.co.uk).

Tags: hacks linux

Blog IndexPosts by TagHome

Copyright 2007-2024 Aaron S. Jackson (compiled: Mon 30 Sep 12:34:19 BST 2024)