Skip to content

Commit

Permalink
fix: fetch all emails from mail server
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Nov 20, 2024
1 parent 55d6823 commit 03864f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@
"label": "Recipients",
"no_copy": 1,
"options": "Mail Recipient",
"read_only": 1
"read_only": 1,
"search_index": 1
},
{
"fieldname": "created_at",
Expand Down Expand Up @@ -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",
Expand Down
32 changes: 22 additions & 10 deletions mail_client/mail_client/doctype/incoming_mail/incoming_mail.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 03864f4

Please sign in to comment.