[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
This commit is contained in:
Rifakat Haradwala (Open ERP)
2011-04-15 17:06:54 +05:30
parent cff7de9d50
commit 10bb8ca3c7
3 changed files with 48 additions and 18 deletions
+27 -13
View File
@@ -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,
+9 -1
View File
@@ -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
+12 -4
View File
@@ -38,8 +38,16 @@
</group>
<field name="references" widget="char" size="4096" groups="base.group_extended"/>
</group>
<separator string="Description" colspan="4"/>
<field name="body" nolabel="1" colspan="4"/>
<notebook colspan="4">
<page string="Body (Text)" >
<field name="body" colspan="4" widget="text" nolabel="1" attrs="{'invisible':[('sub_type','=','html')]}"/>
</page>
<page string="Body (HTML)" >
<field name="body_html" widget="text_html" nolabel="1" colspan="4" attrs="{'invisible':[('sub_type','=','plain')]}"/>
</page>
</notebook>
<separator string="" colspan="4"/>
<group col="6" colspan="4">
<field name="state" colspan="2"/>
@@ -63,8 +71,8 @@
<field name="debug" groups="base.group_extended"/>
<field name="history"/>
<field name="auto_delete"/>
<separator string="xheaders" colspan="4"/>
<field name="headers" colspan="4" nolabel="1" groups="base.group_extended"/>
<separator string="x-headers" colspan="4"/>
<field name="headers" colspan="4" nolabel="1" groups="base.group_extended" height="350"/>
</group>
</page>
</notebook>