Skip to content

Instantly share code, notes, and snippets.

Last active August 9, 2021 09:21
Show Gist options
  • Save bhundven/27a1eea9f1d01056ebb0 to your computer and use it in GitHub Desktop.
Save bhundven/27a1eea9f1d01056ebb0 to your computer and use it in GitHub Desktop.
python mutt+maildir notifier using python
# Based on:
# This version is modified to handle mailboxes with spaces in the names
import pyinotify
import pynotify
from os.path import expanduser
from mailbox import MaildirMessage
from email.header import decode_header
from gtk.gdk import pixbuf_new_from_file
maildir_folder = expanduser(r"~/Mail/")
notification_timeout = 12000
unread_mail_icon = r"/usr/share/icons/oxygen/32x32/status/mail-unread-new.png"
# Getting the path of all the boxes
fd = open(expanduser(r"~/.mutt/mailboxes"), 'r')
# [10:-1] to remove the initial '^mailboxes '
boxes = filter(None, (b.rstrip().replace('+','') for b in fd.readline()[10:-1].split('"')))
# Handling a new mail
icon = pixbuf_new_from_file(unread_mail_icon)
dec_header = lambda h : ' '.join(unicode(s, e if bool(e) else 'ascii') for s, e in decode_header(h))
def newfile(event):
fd = open(event.pathname, 'r')
mail = MaildirMessage(message=fd)
From = "From: " + dec_header(mail['From'])
Subject = "Subject: " + dec_header(mail['Subject'])
n = pynotify.Notification("New mail in "+'/'.join(event.path.split('/')[-3:-1]), From+ "\n"+ Subject)
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, newfile)
for box in boxes:
wm.add_watch(maildir_folder+box+"/new", pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO)
Copy link

I use this with offlineimap+mutt for my gmail account.

Copy link

nestibal commented Jan 5, 2016

Thanks man! I losted this piece of code I writed a long time ago and I'm happy to find it updated here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment