From 529751db69a4d23c9e6187d426880069fc854dec Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 11 Jun 2015 01:53:53 -0400 Subject: [PATCH] [fix] email threading when sender's email client does not support Message-ID passing via In-Reply-To For eg. when a System User is using Outlook and replies to an email from their own client, it reaches the Email Account with the threading info lost and the (sender + subject match) doesn't work because the sender in the first communication was someone different to whom the system user is replying to via the common email account in Frappe. This fix bypasses the sender match when the sender is a system user and subject is atleast 10 chars long (for additional safety) --- .../email/doctype/email_account/email_account.py | 15 ++++++++++++--- frappe/utils/user.py | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) 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"})