瀏覽代碼

handle the case contact_from_header can be None

Son NK 5 年之前
父節點
當前提交
3d4b44dd15
共有 1 個文件被更改,包括 11 次插入1 次删除
  1. 11 1
      email_handler.py

+ 11 - 1
email_handler.py

@@ -123,13 +123,23 @@ def get_or_create_contact(
     """
     contact_from_header is the RFC 2047 format FROM header
     """
+    # contact_from_header can be None, use mail_from in this case instead
+    contact_from_header = contact_from_header or mail_from
+
     # force convert header to string, sometimes contact_from_header is Header object
     contact_from_header = str(contact_from_header)
+
     contact_name, contact_email = parseaddr_unicode(contact_from_header)
     if not contact_email:
-        # From header is empty, try with mail_from
+        # From header is wrongly formatted, try with mail_from
         LOG.warning("From header is empty, parse mail_from %s %s", mail_from, alias)
         contact_name, contact_email = parseaddr_unicode(mail_from)
+        if not contact_email:
+            LOG.error(
+                "Cannot parse contact from from_header:%s, mail_from:%s",
+                contact_from_header,
+                mail_from,
+            )
 
     contact = Contact.get_by(alias_id=alias.id, website_email=contact_email)
     if contact: