From 03864f454dc8f4715fe11be5150d09c6f4966a24 Mon Sep 17 00:00:00 2001 From: s-aga-r Date: Wed, 20 Nov 2024 10:44:17 +0530 Subject: [PATCH] fix: fetch all emails from mail server --- .../doctype/incoming_mail/incoming_mail.json | 5 +-- .../doctype/incoming_mail/incoming_mail.py | 32 +++++++++++++------ .../doctype/outgoing_mail/outgoing_mail.json | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/mail_client/mail_client/doctype/incoming_mail/incoming_mail.json b/mail_client/mail_client/doctype/incoming_mail/incoming_mail.json index a37d6846..615be4d7 100644 --- a/mail_client/mail_client/doctype/incoming_mail/incoming_mail.json +++ b/mail_client/mail_client/doctype/incoming_mail/incoming_mail.json @@ -202,7 +202,8 @@ "label": "Recipients", "no_copy": 1, "options": "Mail Recipient", - "read_only": 1 + "read_only": 1, + "search_index": 1 }, { "fieldname": "created_at", @@ -443,7 +444,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2024-11-18 14:12:48.953159", + "modified": "2024-11-20 09:56:58.726963", "modified_by": "Administrator", "module": "Mail Client", "name": "Incoming Mail", diff --git a/mail_client/mail_client/doctype/incoming_mail/incoming_mail.py b/mail_client/mail_client/doctype/incoming_mail/incoming_mail.py index bcc6ff52..c85aca8d 100644 --- a/mail_client/mail_client/doctype/incoming_mail/incoming_mail.py +++ b/mail_client/mail_client/doctype/incoming_mail/incoming_mail.py @@ -1,6 +1,7 @@ # Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt +import time from email.utils import parseaddr from typing import TYPE_CHECKING @@ -341,26 +342,37 @@ def is_active_mailbox(mailbox: str) -> bool: def fetch_emails_from_mail_server() -> None: """Fetches the emails from the mail server.""" + max_failures = 3 + total_failures = 0 + try: - inbound_api = get_mail_server_inbound_api() - last_synced_at = frappe.db.get_single_value("Mail Client Settings", "last_synced_at") + while True: + inbound_api = get_mail_server_inbound_api() + last_synced_at = frappe.db.get_single_value("Mail Client Settings", "last_synced_at") - if last_synced_at: - last_synced_at = add_or_update_tzinfo(last_synced_at) + if last_synced_at: + last_synced_at = add_or_update_tzinfo(last_synced_at) - result = inbound_api.fetch(last_synced_at=last_synced_at) + result = inbound_api.fetch(last_synced_at=last_synced_at) - for mail in result["mails"]: - process_incoming_mail(mail["incoming_mail_log"], mail["message"], mail["is_spam"]) + frappe.db.set_single_value( + "Mail Client Settings", "last_synced_at", result["last_synced_at"], update_modified=False + ) - frappe.db.set_single_value( - "Mail Client Settings", "last_synced_at", result["last_synced_at"], update_modified=False - ) + if not result["mails"]: + break + + for mail in result["mails"]: + process_incoming_mail(mail["incoming_mail_log"], mail["message"], mail["is_spam"]) except Exception: + total_failures += 1 error_log = frappe.get_traceback(with_context=False) frappe.log_error(title="Fetch Emails from Mail Server", message=error_log) + if total_failures < max_failures: + time.sleep(2**total_failures) + def on_doctype_update() -> None: frappe.db.add_unique( diff --git a/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.json b/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.json index b5f2f149..a78048ad 100644 --- a/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.json +++ b/mail_client/mail_client/doctype/outgoing_mail/outgoing_mail.json @@ -471,7 +471,7 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2024-11-18 13:43:57.642193", + "modified": "2024-11-20 10:43:46.344375", "modified_by": "Administrator", "module": "Mail Client", "name": "Outgoing Mail",