#!/usr/bin/env python import sys if sys.version_info[0] >= 3: import PySimpleGUI as sg else: import PySimpleGUI27 as sg import email import imaplib from datetime import datetime import calendar # Change these to match your email setup LOGIN_EMAIL = 'you@mail.com' LOGIN_PASSWORD = 'your email password' IMAP_SERVER = 'imap-mail.outlook.com' # change to match your email service MAX_EMAILS = 10 def gui(): sg.ChangeLookAndFeel('Topanga') sg.SetOptions(border_width=0, margins=(0,0), element_padding=(4,0)) layout = [[sg.T('Email New Mail Notification'+54*' '), sg.RButton('', image_data=red_x, button_color=('#282923', '#282923'), key='_quit_')], [sg.T('', key='_status_', size=(25,1))],] for i in range(MAX_EMAILS): layout.append ([sg.T('', size=(20,1), key='{}date'.format(i), font='Sans 8'), sg.T('', size=(45,1), font='Sans 8', key='{}from'.format(i))]) window = sg.Window('', no_titlebar=True, grab_anywhere=True, keep_on_top=True, alpha_channel=0, ).Layout(layout).Finalize() # move the window to the upper right corner of the screen w, h = window.GetScreenDimensions() window.Move(w-410,0) window.SetAlpha(.9) window.Refresh() status_elem = window.FindElement('_status_') # The Event Loop while True: status_elem.Update('Reading...') window.Refresh() read_mail(window) status_elem.Update('') event, values = window.Read(timeout=30*1000) if event == '_quit_': break def read_mail(window): """ Reads late emails from IMAP server and displays them in the Window :param window: window to display emails in :return: """ mail = imaplib.IMAP4_SSL(IMAP_SERVER) (retcode, capabilities) = mail.login(LOGIN_EMAIL, LOGIN_PASSWORD) mail.list() typ, data = mail.select('Inbox') n = 0 now = datetime.now() # get messages from today search_string = '(SENTON {}-{}-{})'.format(now.day, calendar.month_abbr[now.month], now.year) (retcode, messages) = mail.search(None, search_string) if retcode == 'OK': msg_list = messages[0].split() # message numbers are separated by spaces, turn into list msg_list.sort(reverse=True) # sort messages descending for n, message in enumerate(msg_list): if n >= MAX_EMAILS: break from_elem = window.FindElement('{}from'.format(n)) date_elem = window.FindElement('{}date'.format(n)) typ, data = mail.fetch(message, '(RFC822)') for response_part in data: if isinstance(response_part, tuple): original = email.message_from_bytes(response_part[1]) date_str = original['Date'] cut_point = date_str.find('-') # a hack to shorten the datetime string if cut_point == -1: cut_point = date_str.find('+') if cut_point == -1: cut_point = len(date_str) date_str = date_str[:cut_point] from_elem.Update(original['From']) date_elem.Update(date_str) window.Refresh() # make the window changes show up right away red_x = "R0lGODlhEAAQAPeQAIsAAI0AAI4AAI8AAJIAAJUAAJQCApkAAJoAAJ4AAJkJCaAAAKYAAKcAAKcCAKcDA6cGAKgAAKsAAKsCAKwAAK0AAK8AAK4CAK8DAqUJAKULAKwLALAAALEAALIAALMAALMDALQAALUAALYAALcEALoAALsAALsCALwAAL8AALkJAL4NAL8NAKoTAKwbAbEQALMVAL0QAL0RAKsREaodHbkQELMsALg2ALk3ALs+ALE2FbgpKbA1Nbc1Nb44N8AAAMIWAMsvAMUgDMcxAKVABb9NBbVJErFYEq1iMrtoMr5kP8BKAMFLAMxKANBBANFCANJFANFEB9JKAMFcANFZANZcANpfAMJUEMZVEc5hAM5pAMluBdRsANR8AM9YOrdERMpIQs1UVMR5WNt8X8VgYMdlZcxtYtx4YNF/btp9eraNf9qXXNCCZsyLeNSLd8SSecySf82kd9qqc9uBgdyBgd+EhN6JgtSIiNuJieGHhOGLg+GKhOKamty1ste4sNO+ueenp+inp+HHrebGrefKuOPTzejWzera1O7b1vLb2/bl4vTu7fbw7ffx7vnz8f///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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