Skip to content

Instantly share code, notes, and snippets.

@michaeldye
Created February 23, 2018 00:11
Show Gist options
  • Save michaeldye/43944682c6eeba3bf6cc1a6fbec893e0 to your computer and use it in GitHub Desktop.
Save michaeldye/43944682c6eeba3bf6cc1a6fbec893e0 to your computer and use it in GitHub Desktop.
Python IMAP mail notifier
#!/usr/bin/env python3
import easyimap
import re
import os
from dateutil.parser import parse
def message_summary(date, title, from_addr):
d = parse(date).strftime('%b %d %I:%M %p') if date is not None else '<unparsed>'
a = re.search('\<(.*)>', from_addr).groups()[0] if '<' in from_addr else from_addr
return '{} | {:40.40} | {:98.98}'.format(d, a, re.sub(r'\r?\n?', '', title))
def main():
try:
imap = easyimap.connect(os.getenv("newmail_host"), os.getenv('newmail_user'), os.getenv('newmail_password'), os.getenv('newmail_mailbox', 'inbox'), read_only=True)
messages = list(map((lambda m: message_summary(m.date, m.title, m.from_addr)), imap.unseen(10)))
if len(messages) > 0:
print("\n".join(messages))
else:
print("[No new messages]")
finally:
imap.quit()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment