Create a gist now

Instantly share code, notes, and snippets.

#!/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 https://github.com/julienXX/terminal-notifier/issues/11
def escape(s):
if s[0] in ('[', '(', '{'):
return '\\' + s
else:
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'])
subprocess.check_call([
os.environ.get('TERMINAL_NOTIFIER', 'terminal-notifier'),
'-title', escape(sender),
'-message', escape(subject),
'-sound', os.environ.get('MAIL_SOUND', 'default'),
])
if __name__ == '__main__':
notify_message(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment