Skip to content

Instantly share code, notes, and snippets.

Last active February 10, 2016 15:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#!/usr/bin/env python
import sys
import os
import re
import email
from email.header import decode_header
from email.utils import parseaddr
import subprocess
def decode(raw):
return ' '.join(s.decode(code).encode('utf-8') if code else s for s, code in decode_header(raw))
def decode_addr(raw):
return ' '.join(decode(s) for s in parseaddr(raw))
# terminal-notifier needs this
# see
def escape(s):
if s[0] in ('[', '(', '{'):
return '\\' + s
return s
def notify_message(message_path):
with open(message_path, 'rb') as f:
msg = email.message_from_file(f)
sender = decode_addr(msg['From'])
subject = decode(msg['Subject'])
os.environ.get('TERMINAL_NOTIFIER', 'terminal-notifier'),
'-title', escape(sender),
'-message', escape(subject),
'-sound', os.environ.get('MAIL_SOUND', 'default'),
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment