#!/usr/bin/python # -*- coding: utf-8 -*- import datetime import os from imap_tools import MailBox def main(): # Server is the address of the imap server print("connecting to imap server") mb = MailBox(os.getenv("MAIL_SERVER"), 993).login( os.getenv("MAIL_USER"), os.getenv("MAIL_PASSWORD") ) # Fetch all emails # Don't mark them as seen # Set bulk=True to read them all into memory in one fetch # (as opposed to in streaming which is slower but uses less memory) print("downloading mailz") messages = mb.fetch(limit=10, mark_seen=False, bulk=True) dates = [m.date for m in messages] median_date = sorted(dates)[len(dates) // 2] median_delta = datetime.now() - median_date.replace(tzinfo=None) oldest_date = sorted(dates)[0] oldest_delta = datetime.now() - oldest_date.replace(tzinfo=None) print(f"Median email: recevied {median_date}, {median_delta} ago") print(f"Oldest email: recevied {oldest_date}, {oldest_delta} ago") from pdb import set_trace; set_trace() if __name__ == "__main__": main()