#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')
'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'
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
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:
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
# 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)
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,
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
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,
'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
</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"/>
<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>