Skip to content

Instantly share code, notes, and snippets.

@tai2
Last active February 10, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tai2/16b8acf511a92638da46 to your computer and use it in GitHub Desktop.
Save tai2/16b8acf511a92638da46 to your computer and use it in GitHub Desktop.
#!/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