Skip to content

Instantly share code, notes, and snippets.

@saroar
Created December 30, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saroar/052d39153427aa676c03ea3c055389a3 to your computer and use it in GitHub Desktop.
Save saroar/052d39153427aa676c03ea3c055389a3 to your computer and use it in GitHub Desktop.
import json
import imaplib
import time
from enum import Enum
class ErrorType(Enum):
login_fail = 1
not_connected = 2
class EmailHandler:
def __init__(self, emails_json='user_missions_are_prt.json'):
self.emails_json = emails_json
def read_emails(self):
with open(self.emails_json, 'r') as file:
emails = json.load(file)
for email in emails:
print(email)
self.__imap_login_and_check_inbox(email)
time.sleep(1)
def __imap_login_and_check_inbox(self, email_data):
email, password = email_data['email_text'], email_data['mail_password']
try:
imap = imaplib.IMAP4_SSL("outlook.office365.com")
imap.login(email, password)
print("Login successful")
imap.select("INBOX")
print("INBOX selected successfully")
imap.logout()
except imaplib.IMAP4.error as e:
error_message = str(e)
print(f"IMAP error: {error_message}")
if "User is authenticated but not connected" in error_message:
error_type = ErrorType.not_connected
else:
error_type = ErrorType.login_fail
self.__process_emails(email_data, error_type)
imap.logout()
def __process_emails(self, item, error_type: ErrorType):
email = item["email_text"]
password = item["mail_password"]
error_file = f"{error_type.name}_emails.json"
try:
with open(error_file, "r") as file:
error_emails = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
error_emails = []
if not any(email_info["email_text"] == email for email_info in error_emails):
error_emails.append({
"id": item["id"],
"email_text": email,
"vfs_password": item["vfs_password"],
"mail_password": password,
"error_type": error_type.name
})
with open(error_file, "w") as file:
json.dump(error_emails, file, indent=4)
# Example usage
handler = EmailHandler()
handler.read_emails()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment