#!/usr/bin/python3 import imaplib import email import datetime import os from pathlib import Path ## constants UNREADDAYS = 3 FILE_SMS = '/tmp/emailcheck.sms' FILE_WARNING = '/tmp/emailcheck.warning' FILE_ESCALATE = '/tmp/emailcheck.ESCALATE' def write_file(path, text): file1 = open(path, "w") file1.write(text) file1.close() # calculate date 3 days ago threedaysago = datetime.datetime.now() - datetime.timedelta(days = UNREADDAYS) threedaysago_str = threedaysago.strftime('(SINCE "%d-%b-%Y")') # print(threedaysago_str) # Connect to inbox imap_server = imaplib.IMAP4_SSL(host='imap.strato.com') imap_server.login('ignace@suy.nl', 'Ghdjh&653gh*') imap_server.select() # Default is `INBOX` #Search for all UIDs first result1, data1 = imap_server.uid('search', threedaysago_str) mails_total = len(data1[0].split()) result2, data2 = imap_server.uid('search', threedaysago_str, 'UNSEEN') mails_unseen = len(data2[0].split()) print("{} mails {}, {}=={}".format(datetime.datetime.now(), threedaysago_str, mails_total, mails_unseen)) # print("{} {}".format(mails_total, mails_unseen)) if (mails_total > mails_unseen) or (mails_total == 0): # some mail has been read in the past days, all is well # delete sms file, warning file if os.path.exists(FILE_SMS): os.remove(FILE_SMS) if os.path.exists(FILE_WARNING): os.remove(FILE_WARNING) if os.path.exists(FILE_ESCALATE): os.remove(FILE_ESCALATE) print(" ->Ok") else: # no mails have been read in the past days # check if a earlier warning file has been created if os.path.exists(FILE_WARNING): if (not os.path.exists(FILE_ESCALATE)): # serious shit, escalate write_file(FILE_ESCALATE, "Escalation has taken place") write_file(FILE_SMS, "SUY-CLOUD: You have received a mail from cloudserver@suy.nl - please check!.") print(" ->Escalate") else: # create a warning file write_file(FILE_WARNING, "In another half day, if no email is read, escalation will take place") write_file(FILE_SMS, "SUY-CLOUD: You have not read mail for 3 days, do this immediately pls or escalate.") print(" ->Warning")