From 10bb8ca3c740933841f830830f5caebd71090b80 Mon Sep 17 00:00:00 2001 From: "Rifakat Haradwala (Open ERP)" Date: Fri, 15 Apr 2011 17:06:54 +0530 Subject: [PATCH] [IMP] mail: improvements for email message fields *added sub_type for the content type of the message *body_html to hold the html part of the message *headers to store all the headers of a message bzr revid: rha@tinyerp.com-20110415113654-pt71ebygq9hx0hdd --- addons/mail/email_message.py | 40 ++++++++++++++++++++++++------------ addons/mail/email_thread.py | 10 ++++++++- addons/mail/email_view.xml | 16 +++++++++++---- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/addons/mail/email_message.py b/addons/mail/email_message.py index a32c29ff759..a0959337dda 100644 --- a/addons/mail/email_message.py +++ b/addons/mail/email_message.py @@ -52,13 +52,14 @@ from email.header import decode_header #import datetime #import tools #import logging -#email_content_types = [ -# 'multipart/mixed', -# 'multipart/alternative', -# 'multipart/related', -# 'text/plain', -# 'text/html' -#] + +email_content_types = [ + ('mixed', 'multipart/mixed'), + ('alternative', 'multipart/alternative'), + ('plain', 'text/plain'), + ('html', 'text/html') +] + LOGGER = netsvc.Logger() _logger = logging.getLogger('mail') @@ -84,11 +85,11 @@ class email_message_common(osv.osv_memory): 'message_id': fields.char('Message Id', size=1024, help="Message Id on Email.", select=1), 'references': fields.text('References', help="References emails."), 'reply_to':fields.char('Reply-To', size=250), - 'sub_type': fields.char('Sub Type', size=32), + 'sub_type': fields.selection(email_content_types, 'Sub Type'), 'headers': fields.text('x_headers'), 'priority':fields.integer('Priority'), 'body': fields.text('Description', translate=True), - 'body_html': fields.text('HTML', help="Contains HTML version of email"), + 'body_html': fields.text('HTML', translate=True, help="Contains HTML version of email"), 'smtp_server_id':fields.many2one('ir.mail_server', 'SMTP Server'), } _rec_name = 'subject' @@ -311,7 +312,7 @@ class email_message(osv.osv): msg_txt['message-id'] = message_id _logger.info('Parsing Message without message-id, generating a random one: %s', message_id) - + fields = msg_txt.keys() msg['id'] = message_id msg['message-id'] = message_id @@ -331,7 +332,7 @@ class email_message(osv.osv): if 'CC' in fields: msg['cc'] = self._decode_header(msg_txt.get('CC')) - if 'Reply-to' in fields: + if 'Reply-To' in fields: msg['reply'] = self._decode_header(msg_txt.get('Reply-To')) if 'Date' in fields: @@ -349,17 +350,29 @@ class email_message(osv.osv): if 'X-Priority' in fields: msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0] #TOFIX: + msg['headers'] = {} + for item in msg_txt.items(): + if item[0].startswith('X-'): + msg['headers'].update({item[0]: item[1]}) if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''): encoding = msg_txt.get_content_charset() body = msg_txt.get_payload(decode=True) if 'text/html' in msg.get('content-type', ''): + msg['body_html'] = body + msg['sub_type'] = 'html' body = tools.html2plaintext(body) + else: + msg['sub_type'] = 'plain' msg['body'] = tools.ustr(body, encoding) attachments = {} has_plain_text = False if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''): body = "" + if 'multipart/alternative' in msg.get('content-type', ''): + msg['sub_type'] = 'alternative' + else: + msg['sub_type'] = 'mixed' for part in msg_txt.walk(): if part.get_content_maintype() == 'multipart': continue @@ -377,10 +390,11 @@ class email_message(osv.osv): # because presumably these are alternatives. content = tools.ustr(content, encoding) if part.get_content_subtype() == 'html': + msg['body_html'] = content body = tools.ustr(tools.html2plaintext(content)) elif part.get_content_subtype() == 'plain': body = content - has_plain_text = True + #has_plain_text = True elif part.get_content_maintype() in ('application', 'image'): if filename : attachments[filename] = part.get_payload(decode=True) @@ -422,7 +436,7 @@ class email_message(osv.osv): subtype=message.sub_type, x_headers=message.headers and eval(message.headers) or {}, priority=message.priority) - res = smtp_server_obj.send_email(cr, uid, + res = smtp_server_obj.send_email(cr, uid, msg, mail_server_id = message.smtp_server_id.id or None, smtp_server=smtp_server and smtp_server.smtp_host or None, diff --git a/addons/mail/email_thread.py b/addons/mail/email_thread.py index 78d5b18886f..7df5085f256 100644 --- a/addons/mail/email_thread.py +++ b/addons/mail/email_thread.py @@ -95,6 +95,10 @@ class email_thread(osv.osv): references = msg.get('references', False) or msg.get('in-reply-to', False), attach = attachments.items(), email_date = msg.get('date'), + body_html= msg.get('body_html', False), + sub_type = msg.get('sub_type', False), + headers = msg.get('headers', False), + reply = msg.get('reply', False), context = context) return res_id @@ -142,7 +146,7 @@ class email_thread(osv.osv): def history(self, cr, uid, threads, keyword, history=False, subject=None, email=False, details=None, \ email_from=False, message_id=False, references=None, attach=None, email_cc=None, \ - email_bcc=None, email_date=None, context=None): + email_bcc=None, email_date=None, body_html=None, sub_type=None, headers=None, reply=None, context=None): """ @param self: The object pointer @param cr: the current row, from the database cursor, @@ -233,6 +237,10 @@ class email_thread(osv.osv): 'message_id': message_id, 'attachment_ids': [(6, 0, attachments)], 'state' : 'received', + 'body_html': body_html, + 'sub_type': sub_type, + 'headers': headers, + 'reply_to': reply } obj.create(cr, uid, data, context=context) return True diff --git a/addons/mail/email_view.xml b/addons/mail/email_view.xml index 6f0a17be442..7e52ec5a0af 100644 --- a/addons/mail/email_view.xml +++ b/addons/mail/email_view.xml @@ -38,8 +38,16 @@ - - + + + + + + + + + + @@ -63,8 +71,8 @@ - - + +