Update for multiple accounts

This commit is contained in:
Alexander Hosking 2023-01-08 10:11:40 -05:00
parent 4a8f30ff0b
commit da79194e87

View File

@ -1,37 +1,42 @@
import os import os
import imaplib import imaplib
import time import json
import re
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
print(os.environ.get("APP_PASSWORD")) EMAIL_ACCOUNTS = json.loads(os.environ.get("MAIL_ACCOUNTS"))
EMAIL_PASSWORDS = json.loads(os.environ.get("MAIL_PASSWORDS"))
# using zip()
# to convert lists to dictionary
accounts = dict(zip(EMAIL_ACCOUNTS, EMAIL_PASSWORDS))
print(accounts)
################ IMAP SSL ############################## ################ IMAP SSL ##############################
with imaplib.IMAP4_SSL(host="imap.gmail.com", port=imaplib.IMAP4_SSL_PORT) as imap_ssl: for account in accounts:
print("Connection Object : {}".format(imap_ssl)) with imaplib.IMAP4_SSL(host="imap.gmail.com", port=imaplib.IMAP4_SSL_PORT) as imap_ssl:
print("Logging into mailbox...") print("Connection Object : {}".format(imap_ssl))
try: print("Logging into mailbox...")
resp_code, response = imap_ssl.login("alexander@ahosking.com", os.environ.get("APP_PASSWORD")) try:
except Exception as e: resp_code, response = imap_ssl.login(account, accounts[account])
print("ErrorType : {}, Error : {}".format(type(e).__name__, e)) except Exception as e:
resp_code, response = None, None print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
resp_code, response = None, None
print("Response Code : {}".format(resp_code)) print("Response Code : {}".format(resp_code))
print("Response : {}\n".format(response[0].decode())) print("Response : {}\n".format(response[0].decode()))
imap_ssl.select('inbox') imap_ssl.select('inbox')
resp_code, messages = imap_ssl.search(None, 'UnSeen') resp_code, messages = imap_ssl.search(None, 'UnSeen')
if resp_code == 'OK': if resp_code == 'OK':
if len(messages[0].split()) > 0: if len(messages[0].split()) > 0:
print('True') print('True')
print(messages[0].split()) print(messages[0].split())
print(len(messages[0].split())) print(len(messages[0].split()))
else: else:
print('False') print('False')
############### Logout of Mailbox ###################### ############### Logout of Mailbox ######################
print("\nLogging Out....") print("\nLogging Out....")
imap_ssl.close() imap_ssl.close()