[fix] inline images in email receive

This commit is contained in:
Rushabh Mehta 2015-11-06 16:49:17 +05:30
parent bc8882d6c7
commit fca5b4d002
3 changed files with 33 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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()