def test_clean_email_html(self): from frappe.utils.html_utils import clean_email_html sample = '''<script>a=b</script><h1>Hello</h1><p>Para</p>''' clean = clean_email_html(sample) self.assertFalse('<script>' in clean) self.assertTrue('<h1>Hello</h1>' in clean) sample = '''<style>body { font-family: Arial }</style><h1>Hello</h1><p>Para</p>''' clean = clean_email_html(sample) self.assertFalse('<style>' in clean) self.assertTrue('<h1>Hello</h1>' in clean) sample = '''<h1>Hello</h1><p>Para</p><a href="http://test.com">text</a>''' clean = clean_email_html(sample) self.assertTrue('<h1>Hello</h1>' in clean) self.assertTrue('<a href="http://test.com">text</a>' in clean)
def test_clean_email_html(self): from frappe.utils.html_utils import clean_email_html sample = '''<script>a=b</script><h1>Hello</h1><p>Para</p>''' clean = clean_email_html(sample) self.assertFalse('<script>' in clean) self.assertTrue('<h1>Hello</h1>' in clean) sample = '''<style>body { font-family: Arial }</style><h1>Hello</h1><p>Para</p>''' clean = clean_email_html(sample) self.assertFalse('<style>' in clean) self.assertTrue('<h1>Hello</h1>' in clean) sample = '''<h1>Hello</h1><p>Para</p><a href="http://test.com">text</a>''' clean = clean_email_html(sample) self.assertTrue('<h1>Hello</h1>' in clean) self.assertTrue('<a href="http://test.com">text</a>' in clean)
def add_comment(reference_doctype, reference_name, content, comment_email): """allow any logged user to post a comment""" doc = frappe.get_doc( dict(doctype='Comment', reference_doctype=reference_doctype, reference_name=reference_name, content=clean_email_html(content), comment_email=comment_email)).insert(ignore_permissions=True) return doc.as_dict()
def add_comment(doc): """allow any logged user to post a comment""" doc = frappe.get_doc(json.loads(doc)) doc.content = clean_email_html(doc.content) if not (doc.doctype=="Communication" and doc.communication_type=='Comment'): frappe.throw(_("This method can only be used to create a Comment"), frappe.PermissionError) doc.insert(ignore_permissions=True) return doc.as_dict()
def add_comment(doc): """allow any logged user to post a comment""" doc = frappe.get_doc(json.loads(doc)) doc.content = clean_email_html(doc.content) if not (doc.doctype=="Communication" and doc.communication_type=='Comment'): frappe.throw(_("This method can only be used to create a Comment"), frappe.PermissionError) doc.insert(ignore_permissions=True) return doc.as_dict()
def insert_communication(self, msg, args=None): if isinstance(msg, list): raw, uid, seen = msg else: raw = msg uid = -1 seen = 0 if isinstance(args, dict): if args.get("uid", -1): uid = args.get("uid", -1) if args.get("seen", 0): seen = args.get("seen", 0) email = Email(raw) if email.from_email == self.email_id and not email.mail.get( "Reply-To"): # gmail shows sent emails in inbox # and we don't want emails sent by us to be pulled back into the system again # dont count emails sent by the system get those if frappe.flags.in_test: print( 'WARN: Cannot pull email. Sender sames as recipient inbox') raise SentEmailInInbox if email.message_id: # https://stackoverflow.com/a/18367248 names = frappe.db.sql( """SELECT DISTINCT `name`, `creation` FROM `tabCommunication` WHERE `message_id`='{message_id}' ORDER BY `creation` DESC LIMIT 1""".format(message_id=email.message_id), as_dict=True) if names: name = names[0].get("name") # email is already available update communication uid instead frappe.db.set_value("Communication", name, "uid", uid, update_modified=False) return frappe.get_doc("Communication", name) if email.content_type == 'text/html': email.content = clean_email_html(email.content) communication = frappe.get_doc({ "doctype": "Communication", "subject": email.subject, "content": email.content, 'text_content': email.text_content, "sent_or_received": "Received", "sender_full_name": email.from_real_name, "sender": email.from_email, "recipients": email.mail.get("To"), "cc": email.mail.get("CC"), "email_account": self.name, "communication_medium": "Email", "uid": int(uid or -1), "message_id": email.message_id, "communication_date": email.date, "has_attachment": 1 if email.attachments else 0, "seen": seen or 0 }) self.set_thread(communication, email) if communication.seen: # get email account user and set communication as seen users = frappe.get_all("User Email", filters={"email_account": self.name}, fields=["parent"]) users = list(set([user.get("parent") for user in users])) communication._seen = json.dumps(users) communication.flags.in_receive = True communication.insert(ignore_permissions=True) # save attachments communication._attachments = email.save_attachments_in_doc( communication) # replace inline images dirty = False for file in communication._attachments: if file.name in email.cid_map and email.cid_map[file.name]: dirty = True email.content = email.content.replace( "cid:{0}".format(email.cid_map[file.name]), file.file_url) if dirty: # not sure if using save() will trigger anything communication.db_set("content", sanitize_html(email.content)) # notify all participants of this thread if self.enable_auto_reply and getattr(communication, "is_first", False): self.send_auto_reply(communication, email) return communication
def get_content(self): if self.content_type == 'text/html': return clean_email_html(self.content)
def insert_communication(self, msg, args={}): if isinstance(msg, list): raw, uid, seen = msg else: raw = msg uid = -1 seen = 0 if args.get("uid", -1): uid = args.get("uid", -1) if args.get("seen", 0): seen = args.get("seen", 0) email = Email(raw) if email.from_email == self.email_id and not email.mail.get("Reply-To"): # gmail shows sent emails in inbox # and we don't want emails sent by us to be pulled back into the system again # dont count emails sent by the system get those if frappe.flags.in_test: print('WARN: Cannot pull email. Sender sames as recipient inbox') raise SentEmailInInbox if email.message_id: names = frappe.db.sql("""select distinct name from tabCommunication where message_id='{message_id}' order by creation desc limit 1""".format( message_id=email.message_id ), as_dict=True) if names: name = names[0].get("name") # email is already available update communication uid instead frappe.db.set_value("Communication", name, "uid", uid, update_modified=False) return if email.content_type == 'text/html': email.content = clean_email_html(email.content) communication = frappe.get_doc({ "doctype": "Communication", "subject": email.subject, "content": email.content, 'text_content': email.text_content, "sent_or_received": "Received", "sender_full_name": email.from_real_name, "sender": email.from_email, "recipients": email.mail.get("To"), "cc": email.mail.get("CC"), "email_account": self.name, "communication_medium": "Email", "uid": int(uid or -1), "message_id": email.message_id, "communication_date": email.date, "has_attachment": 1 if email.attachments else 0, "seen": seen or 0 }) self.set_thread(communication, email) if communication.seen: # get email account user and set communication as seen users = frappe.get_all("User Email", filters={ "email_account": self.name }, fields=["parent"]) users = list(set([ user.get("parent") for user in users ])) communication._seen = json.dumps(users) communication.flags.in_receive = True communication.insert(ignore_permissions = 1) # save attachments communication._attachments = email.save_attachments_in_doc(communication) # replace inline images dirty = False for file in communication._attachments: if file.name in email.cid_map and email.cid_map[file.name]: dirty = True email.content = email.content.replace("cid:{0}".format(email.cid_map[file.name]), file.file_url) if dirty: # not sure if using save() will trigger anything communication.db_set("content", sanitize_html(email.content)) # notify all participants of this thread if self.enable_auto_reply and getattr(communication, "is_first", False): self.send_auto_reply(communication, email) return communication