From 6644159440899f41926a8972adde81b1de088554 Mon Sep 17 00:00:00 2001 From: Hussain Nagaria <34810212+NagariaHussain@users.noreply.github.com> Date: Tue, 16 Sep 2025 11:06:26 +0530 Subject: [PATCH] fix: gracefully handle missing `from` header in email (#33996) --- frappe/email/receive.py | 13 ++++++++++--- frappe/utils/__init__.py | 4 +++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/frappe/email/receive.py b/frappe/email/receive.py index a0409c7909..a2598546e2 100644 --- a/frappe/email/receive.py +++ b/frappe/email/receive.py @@ -446,6 +446,15 @@ class Email: _from_email = self.decode_email(self.mail.get("X-Original-From") or self.mail["From"]) _reply_to = self.decode_email(self.mail.get("Reply-To")) + if not _from_email: + # happens in some cases when email server is misconfigured + # should not fail the entire syncing process + frappe.log_error( + f"Email missing `From` header. UID: {getattr(self, 'uid', 'unknown')}", str(self.mail) + ) + self.from_email = None + return + if _reply_to and not frappe.db.get_value( "Email Account", {"email_id": _reply_to, "enable_incoming": 1}, "email_id" ): @@ -453,9 +462,7 @@ class Email: else: self.from_email = extract_email_id(_from_email) - if self.from_email: - self.from_email = self.from_email.lower() - + self.from_email = self.from_email.lower() self.from_real_name = parse_addr(_from_email)[0] if "@" in _from_email else _from_email @staticmethod diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index 0279fb9c85..bad08b3565 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -105,8 +105,10 @@ def get_formatted_email(user, mail=None): return cstr(make_header(decode_header(formataddr((fullname, mail))))) -def extract_email_id(email): +def extract_email_id(email: str) -> str: """fetch only the email part of the Email Address""" + if not email: + return "" return cstr(parse_addr(email)[1])