From fca5b4d002ff2cb59893aab24d9a47cd77d0c0ea Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 6 Nov 2015 16:49:17 +0530 Subject: [PATCH] [fix] inline images in email receive --- .../doctype/email_account/email_account.py | 19 ++++++++++++----- frappe/email/receive.py | 6 ++++++ frappe/utils/file_manager.py | 21 ++++++++++++------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 7478fea9f7..a2b65f02c8 100644 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -11,7 +11,7 @@ from frappe.utils.jinja import render_template from frappe.email.smtp import SMTPServer from frappe.email.receive import POP3Server, Email from poplib import error_proto -import markdown2, re +import re from dateutil.relativedelta import relativedelta from datetime import datetime, timedelta @@ -172,12 +172,21 @@ class EmailAccount(Document): # save attachments communication._attachments = email.save_attachments_in_doc(communication) - if self.enable_auto_reply and getattr(communication, "is_first", False): - self.send_auto_reply(communication, email) + # replace inline images + dirty = False + for file_name in communication._attachments: + if email.cid_map[file_name]: + dirty = True + communication.content = communication.content.replace("cid:{0}".format(email.cid_map[file_name]), + email.file_name_map[file_name]) + + if dirty: + # not sure if using save() will trigger anything + communication.db_set("content", communication.content) # notify all participants of this thread - # convert content to HTML - by default text parts of replies are used. - communication.content = markdown2.markdown(communication.content) + if self.enable_auto_reply and getattr(communication, "is_first", False): + self.send_auto_reply(communication, email) return communication diff --git a/frappe/email/receive.py b/frappe/email/receive.py index 3042c12d0e..030de8a36b 100644 --- a/frappe/email/receive.py +++ b/frappe/email/receive.py @@ -203,7 +203,9 @@ class Email: self.text_content = '' self.html_content = '' self.attachments = [] + self.cid_map = {} self.parse() + self.file_name_map = {} self.set_content_and_type() self.set_subject() @@ -293,6 +295,8 @@ class Email: 'fcontent': fcontent, }) + self.cid_map[fname] = part.get("Content-Id").strip("><") + def save_attachments_in_doc(self, doc): """Save email attachments in given document.""" from frappe.utils.file_manager import save_file, MaxFileSizeReachedError @@ -303,6 +307,8 @@ class Email: file_data = save_file(attachment['fname'], attachment['fcontent'], doc.doctype, doc.name) saved_attachments.append(file_data.file_name) + + self.file_name_map[file_data.file_name] = file_data.file_url except MaxFileSizeReachedError: # WARNING: bypass max file size exception pass diff --git a/frappe/utils/file_manager.py b/frappe/utils/file_manager.py index 56cbd6c731..461d4db365 100644 --- a/frappe/utils/file_manager.py +++ b/frappe/utils/file_manager.py @@ -272,23 +272,28 @@ def delete_file(path): os.remove(path) def get_file(fname): + """Returns [`file_name`, `content`] for given file name `fname`""" + file_path = get_file_path(fname) + + # read the file + with open(get_site_path("public", file_path), 'r') as f: + content = f.read() + + return [file_path.rsplit("/", 1)[-1], content] + +def get_file_path(file_name): + """Returns file path from given file name""" f = frappe.db.sql("""select file_name from `tabFile` - where name=%s or file_name=%s""", (fname, fname)) + where name=%s or file_name=%s""", (file_name, file_name)) if f: file_name = f[0][0] - else: - file_name = fname file_path = file_name if not "/" in file_path: file_path = "files/" + file_path - # read the file - with open(get_site_path("public", file_path), 'r') as f: - content = f.read() - - return [file_name, content] + return file_path def get_content_hash(content): return hashlib.md5(content).hexdigest()