import imaplib import re import time import subprocess ## enter your account details bellow! imapServer = "your.imap.server" port = "993" username = "mail@blabla" password = "dont_be_stupid_and_paste_your_pass" ##how often to check? give interval in seconds! Checking too often might affect performance and stability. checkIntevaral = 120 Mailbox = imaplib.IMAP4_SSL(imapServer, port) rc,resp = Mailbox.login(username,password) if rc == 'OK': print("Connected to mail-server "+imapServer) rc, message = Mailbox.status('INBOX', "(UNSEEN)") unreadCount = int(re.search("UNSEEN (\d+)",str( message[0])).group(1)) oldValue = 0 file = open("/tmp/mailnotify.tmp", "w+") file.write(str(unreadCount)) file.close while(1): rc, message = Mailbox.status('INBOX', "(UNSEEN)") unreadCount = int(re.search("UNSEEN (\d+)",str( message[0])).group(1)) file = open("/tmp/mailnotify.tmp", "r+") oldValue = int(file.readline()) file.close() if (unreadCount>oldValue): subprocess.call(["notify-send", "-u", "low", "-t", "5000", "New email!" ,"You have "+str(unreadCount)+" unread "+ "emails!" if unreadCount > 1 else "email!", "--icon=email"]) if oldValue != unreadCount: file = open("/tmp/mailnotify.tmp", "w+") file.write(str(unreadCount)) file.close() time.sleep(checkIntevaral) else : print('Fail to connect') Mailbox.logout() file.remove()