markhuge MH\n s

New Mail Notification with OfflineIMAP (or any Maildir)


signed with 0x683A22F9469CA4EB


There’s a really simple way to check for new msgs that will satisfy 90% of people who search for this:

  find $HOME/mail/*/INBOX/new/ -type f | wc -l

If that’s all you need, you’re welcome.

I’m a weirdo and I have really specific needs. I don’t care about new (unread) messages, I care about new msgs between syncs (unread or not). I also want a notification when I’ve started a sync and when it’s complete.

To do this, I wrapped offlineimap with a shell script:


# Count mail
count() { 
  find "$HOME"/mail/*/INBOX -type f | wc -l

# Make sure offlineimap isn't already running
if [ -z "$(pgrep offlineimap)" ]; then

  # Default msg
  msg="Done syncing mail"

  notify-send "Syncing mail..."

  # Get old email count

  # Get email count after sync

  # Overwrite msg if new emails are found
  if [ "$new" -gt "$old" ]; then
    msg="$((new - old)) new emails"

  notify-send "${msg}"

  notify-send "Mail sync already running"

About the Author

Mark Wilkerson


R&D engineer @ Twitch. Previously Blizzard, Hightail, Co-Founder @ SpeakUp

Mark is building open source tools to make web3 and decentralized self-ownership easy for regular people.

Live dev streams at

More Posts