#! /usr/bin/env python3 # -*- coding: utf-8 -*- import os, sys, imaplib, email from email.header import decode_header from email.utils import parseaddr import sqlite3 as sqlite from datetime import datetime _db_file = os.path.join(os.path.expanduser("~"), ".config/ImapQuickCheck/accounts.db") markAllReadRequested = False deleteAllReadRequested = False readonly_state=1 if len(sys.argv) > 1: _db_file = sys.argv[1] if "-markread" in sys.argv: markAllReadRequested = True readonly_state=0 if "-deleteread" in sys.argv: deleteAllReadRequested = True readonly_state=0 if not os.path.exists(_db_file): print("ERROR : Could not find .db accounts file. BYE") sys.exit(1) _keys_new = ["From:", "Subject:", "Date:"] def imapMailboxChecker(): """ CONNECT IMAP SERVER """ if _connect_mode == 1: conn = imaplib.IMAP4(host=_host, port=_port) elif _connect_mode == 2: conn = imaplib.IMAP4_SSL(host=_host, port=_port) try: (retcode, capabilities) = conn.login(_login, _pass) except: print(sys.exc_info()[0]) print("__ERROR_LOGIN:", _account_name) return False try: conn.select('inbox', readonly=readonly_state) result, data = conn.uid('search', None, 'UNSEEN') email_ids = data[0].split() newmailnum = len(data[0].split()) except: return False """ RETRIEVE UNSEEN MESSAGES DETAILS """ try: if result == 'OK': # os.system('zenity --width=220 --info --text="HERE1" --timeout 1 2>/dev/null') if markAllReadRequested: for num in email_ids: conn.uid('store', num, '+FLAGS', '(\\Seen)') elif deleteAllReadRequested: for num in email_ids: conn.uid('STORE', num, '+X-GM-LABELS', '(\\Trash)') conn.expunge() else: mailArray = [] if newmailnum > 0: for message in email_ids: (ret, mesginfo) = conn.uid('fetch', message, '(BODY[HEADER.FIELDS (SUBJECT FROM DATE)])') if ret == 'OK': email_body = mesginfo[0][1] mail = email.message_from_bytes(email_body) email_subject = decode_header(mail["Subject"])[0][0] email_from = email.utils.parseaddr(mail['From'])[1] email_time = datetime.fromtimestamp(email.utils.mktime_tz(email.utils.parsedate_tz(mail['Date']))) mailArray.append( [ email_from, email_subject, email_time ] ) else: print (_host, "IMAP : request NOt Ok") mailArray.reverse() print ("__ACCOUNT:", _account_name) print ("__UNREAD:", str(len(mailArray))) for j in range(0, len(mailArray)): print ("__SENDER:", mailArray[j][0]) print ("__SUBJECT:", mailArray[j][1]) print ("__DATE:", mailArray[j][2]) print ("____________") conn.close() return True except: return False """ MAIN LOOP """ con = sqlite.connect(_db_file) cur = con.cursor() cur.execute("SELECT type AS connect_type, name, host, port, user, pwd, inbox FROM accounts ORDER BY id") for (connect_type, name, host, port, user, pwd, inbox) in cur: #print '%s is %d years old.' % (name, id) _connect_mode = connect_type _account_name = name _host = host _port = port _login = user _pass = pwd _inbox = inbox imapMailboxChecker()