Commit ecb0e271 authored by Kevin Deldycke's avatar Kevin Deldycke

Add useful methods to get a text with good encoding.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@5955 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 556bd364
...@@ -90,6 +90,9 @@ class MailMessage(XMLObject, Event, CMFMailInMessage): ...@@ -90,6 +90,9 @@ class MailMessage(XMLObject, Event, CMFMailInMessage):
XMLObject._edit(self, *args, **kw) XMLObject._edit(self, *args, **kw)
def cleanIncomingMessage(self, **kw): def cleanIncomingMessage(self, **kw):
"""
Clean up the the message data that came from the portal_mailin tool.
"""
# Delete attachments # Delete attachments
attachments = kw.get('attachments', {}) attachments = kw.get('attachments', {})
if kw.has_key('attachments'): if kw.has_key('attachments'):
...@@ -104,6 +107,49 @@ class MailMessage(XMLObject, Event, CMFMailInMessage): ...@@ -104,6 +107,49 @@ class MailMessage(XMLObject, Event, CMFMailInMessage):
del kw['header']['content-transfer-encoding'] del kw['header']['content-transfer-encoding']
return kw return kw
def getBodyEncoding(self):
"""
Extract the charset encoding from mail header.
"""
charset = None
header = self.getHeader()
if header != None and header.has_key('content-type'):
content_type = header['content-type'].replace('\n', ' ')
content_type_info = content_type.split(';')
for ct_info in content_type_info:
info = ct_info.strip().lower()
if info.startswith('charset='):
charset = info[len('charset='):]
# Some charset statements are quoted
if charset.startswith('"') or charset.startswith("'"): charset = charset[1:]
if charset.endswith( '"') or charset.endswith( "'"): charset = charset[:-1]
break
return charset
def getEncodedBody(self, output_charset="utf-8"):
"""
Return the entire body message encoded in the given charset.
"""
body_charset = self.getBodyEncoding()
if body_charset == None or body_charset.lower() == output_charset.lower():
return self.getBody()
else:
unicode_body = unicode(self.getBody(), body_charset)
return unicode_body.encode(output_charset)
def getHeader(self):
"""
Get the header dict of the message.
"""
header = self.header
if header == None or type(header) == type({}):
return header
elif type(header) == type(''):
# Must do an 'eval' because the header is a dict stored as a text (see ERP5/PropertySheet/MailMessage.py)i
return eval(header)
else:
raise 'TypeError', "Type of 'header' property can't be determined."
def send(self, from_url=None, to_url=None, msg=None, subject=None): def send(self, from_url=None, to_url=None, msg=None, subject=None):
""" """
Sends a reply to this mail message. Sends a reply to this mail message.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment