From b85545bb9c154178800da4fbbb2b33814c7836a8 Mon Sep 17 00:00:00 2001 From: Stefano Marinelli Date: Sun, 22 Oct 2023 19:54:25 +0200 Subject: [PATCH] Some server send a BYE or drop the connection after less than 10 minutes (for example, Gmail). The socket timeout has been reduced to 8 minutes and there's now some logic to deal with the BYE IMAP message --- NotiMail.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/NotiMail.py b/NotiMail.py index 4cc279b..a7e494e 100644 --- a/NotiMail.py +++ b/NotiMail.py @@ -288,6 +288,8 @@ class IMAPHandler: line = self.mail.readline() if line: print(line.decode('utf-8')) + if b'BYE' in line: + raise ConnectionAbortedError("Received BYE from server. Trying to reconnect...") if b'EXISTS' in line: break self.mail.send(b'DONE\r\n') @@ -330,23 +332,24 @@ class MultiIMAPHandler: for thread in threads: thread.join() - @staticmethod def monitor_account(handler): print(f"Monitoring {handler.email_user}") logging.info(f"Monitoring {handler.email_user}") - try: - handler.connect() - while True: - handler.idle() - handler.process_emails() - except ConnectionAbortedError as e: - print(str(e)) - time.sleep(30) - except Exception as e: - print(f"An unexpected error occurred: {str(e)}") - logging.error(f"An unexpected error occurred: {str(e)}") - notifier.send_notification("Script Error", f"An unexpected error occurred: {str(e)}") + while True: # Add a loop to keep retrying on connection loss + try: + handler.connect() + while True: + handler.idle() + handler.process_emails() + except ConnectionAbortedError as e: + print(str(e)) + time.sleep(30) # Sleep for 30 seconds before retrying + except Exception as e: + print(f"An unexpected error occurred: {str(e)}") + logging.error(f"An unexpected error occurred: {str(e)}") + notifier.send_notification("Script Error", f"An unexpected error occurred: {str(e)}") + break def shutdown_handler(signum, frame): print("Shutdown signal received. Cleaning up...") @@ -410,7 +413,7 @@ def multi_account_main(): global notifier notifier = Notifier(providers) - socket.setdefaulttimeout(600) + socket.setdefaulttimeout(480) signal.signal(signal.SIGTERM, shutdown_handler) signal.signal(signal.SIGINT, shutdown_handler)