diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index ba2f178424..bf1bd4fdd9 100644 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -192,18 +192,27 @@ class EmailAccount(Document): parent = frappe.get_doc(parent.reference_doctype, parent.reference_name) - if not parent and self.append_to and subject_field and sender_field: - # try and match by subject and sender - # if sent by same sender with same subject, - # append it to old coversation - - subject = re.sub("Re[^:]*:\s*", "", email.subject) - - parent = frappe.db.get_all(self.append_to, filters={ - sender_field: email.from_email, - subject_field: ("like", "%{0}%".format(subject)), - "creation": (">", (get_datetime() - relativedelta(days=10)).strftime(DATE_FORMAT)) - }, fields="name") + if not parent and self.append_to and sender_field: + if subject_field: + # try and match by subject and sender + # if sent by same sender with same subject, + # append it to old coversation + + subject = re.sub("Re[^:]*:\s*", "", email.subject) + + parent = frappe.db.get_all(self.append_to, filters={ + sender_field: email.from_email, + subject_field: ("like", "%{0}%".format(subject)), + "creation": (">", (get_datetime() - relativedelta(days=10)).strftime(DATE_FORMAT)) + }, fields="name") + + else: + # try and match by sender only + # as there is no subject field, it implies that threading isn't by subject, but by sender only + + parent = frappe.db.get_all(self.append_to, filters={ + sender_field: email.from_email, + }, fields="name") if parent: parent = frappe.get_doc(self.append_to, parent[0].name)