diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index e6a7449a3c..04be4ed5cf 100644 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -5,7 +5,8 @@ from __future__ import unicode_literals import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import validate_email_add, cint, get_datetime, DATE_FORMAT +from frappe.utils import validate_email_add, cint, get_datetime, DATE_FORMAT, strip +from frappe.utils.user import is_system_user from frappe.email.smtp import SMTPServer from frappe.email.receive import POP3Server, Email from poplib import error_proto @@ -204,8 +205,7 @@ class EmailAccount(Document): # try and match by subject and sender # if sent by same sender with same subject, # append it to old coversation - - subject = re.sub("(Re|RE)[^:]*:\s*", "", email.subject) + subject = strip(re.sub("^\s*(Re|RE)[^:]*:\s*", "", email.subject)) parent = frappe.db.get_all(self.append_to, filters={ sender_field: email.from_email, @@ -213,6 +213,15 @@ class EmailAccount(Document): "creation": (">", (get_datetime() - relativedelta(days=10)).strftime(DATE_FORMAT)) }, fields="name") + # match only subject field + # when the from_email is of a user in the system + # and subject is atleast 10 chars long + if not parent and len(subject) > 10 and is_system_user(email.from_email): + parent = frappe.db.get_all(self.append_to, filters={ + 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 diff --git a/frappe/utils/user.py b/frappe/utils/user.py index 98cce5297c..64b00f4e56 100644 --- a/frappe/utils/user.py +++ b/frappe/utils/user.py @@ -285,3 +285,6 @@ def get_enabled_system_users(): def is_website_user(): return frappe.get_user().doc.user_type == "Website User" + +def is_system_user(username): + return frappe.db.get_value("User", {"name": username, "enabled": 1, "user_type": "System User"})