From 9697f8b04341af6e76937c14d38255813219660b Mon Sep 17 00:00:00 2001 From: Stephane Wirtel Date: Thu, 6 May 2010 14:28:19 +0200 Subject: [FIX] fetchmail: Decode correctly the body and the subject of the emails bzr revid: stephane@openerp.com-20100506122819-2c2ya2rln710l048 --- addons/fetchmail/fetchmail.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'addons/fetchmail/fetchmail.py') diff --git a/addons/fetchmail/fetchmail.py b/addons/fetchmail/fetchmail.py index 598ed4aad6f..832a2ffd910 100644 --- a/addons/fetchmail/fetchmail.py +++ b/addons/fetchmail/fetchmail.py @@ -184,7 +184,7 @@ class email_server(osv.osv): history_pool = self.pool.get('mail.server.history') msg_txt = email.message_from_string(message) message_id = msg_txt.get('Message-ID', False) - + msg = {} if not message_id: return False @@ -195,7 +195,7 @@ class email_server(osv.osv): msg['message-id'] = message_id if 'Subject' in fields: - msg['subject'] = msg_txt.get('Subject') + msg['subject'] = ' '.join(map(lambda (x, y): unicode(x, y or 'ascii'), decode_header(msg_txt.get('Subject')))) if 'Content-Type' in fields: msg['content-type'] = msg_txt.get('Content-Type') @@ -259,6 +259,9 @@ class email_server(osv.osv): msg['body'] = body msg['attachments'] = attachents + encoding = msg_txt.get_content_charset('utf-8') + msg['body'] = msg['body'].decode(encoding).encode('utf-8') + res_id = False if msg.get('references', False): id = False -- cgit v1.2.3 From a6a41d78b0f513b6416c98b45c3ec7f55309ff87 Mon Sep 17 00:00:00 2001 From: Stephane Wirtel Date: Thu, 6 May 2010 16:28:27 +0200 Subject: [FIX] fetchmail: Decode correcly the body and the subject of the emails (bis) bzr revid: stephane@openerp.com-20100506142827-o8gm55f0ykqmfie8 --- addons/fetchmail/fetchmail.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'addons/fetchmail/fetchmail.py') diff --git a/addons/fetchmail/fetchmail.py b/addons/fetchmail/fetchmail.py index 832a2ffd910..564c1c11e54 100644 --- a/addons/fetchmail/fetchmail.py +++ b/addons/fetchmail/fetchmail.py @@ -193,24 +193,27 @@ class email_server(osv.osv): msg['id'] = message_id msg['message-id'] = message_id + + def _decode_header(txt): + return ' '.join(map(lambda (x, y): unicode(x, y or 'ascii'), decode_header(txt))) if 'Subject' in fields: - msg['subject'] = ' '.join(map(lambda (x, y): unicode(x, y or 'ascii'), decode_header(msg_txt.get('Subject')))) + msg['subject'] = _decode_header(msg_txt.get('Subject')) if 'Content-Type' in fields: msg['content-type'] = msg_txt.get('Content-Type') if 'From' in fields: - msg['from'] = msg_txt.get('From') + msg['from'] = _decode_header(msg_txt.get('From')) if 'Delivered-To' in fields: - msg['to'] = msg_txt.get('Delivered-To') + msg['to'] = _decode_header(msg_txt.get('Delivered-To')) if 'Cc' in fields: - msg['cc'] = msg_txt.get('Cc') + msg['cc'] = _decode_header(msg_txt.get('Cc')) if 'Reply-To' in fields: - msg['reply'] = msg_txt.get('Reply-To') + msg['reply'] = _decode_header(msg_txt.get('Reply-To')) if 'Date' in fields: msg['date'] = msg_txt.get('Date') @@ -228,7 +231,10 @@ class email_server(osv.osv): msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0] if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', None): + encoding = msg_txt.get_content_charset() msg['body'] = msg_txt.get_payload(decode=True) + if encoding: + msg['body'] = msg['body'].decode(encoding).encode('utf-8') attachents = {} if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', None): @@ -238,29 +244,34 @@ class email_server(osv.osv): if part.get_content_maintype() == 'multipart': continue + encoding = part.get_content_charset() + if part.get_content_maintype()=='text': content = part.get_payload(decode=True) - if part.get_content_subtype() == 'html': - body = html2plaintext(content) - elif part.get_content_subtype() == 'plain': - body = content - filename = part.get_filename() if filename : - attachents[filename] = part.get_payload(decode=True) - + attachents[filename] = content + else: + if encoding: + content = unicode(content, encoding) + if part.get_content_subtype() == 'html': + body = html2plaintext(content) + elif part.get_content_subtype() == 'plain': + body = content elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text': filename = part.get_filename(); if filename : attachents[filename] = part.get_payload(decode=True) else: - body += part.get_payload(decode=True) + res = part.get_payload(decode=True) + if encoding: + res = res.decode(encoding).encode('utf-8') + + body += res msg['body'] = body msg['attachments'] = attachents - encoding = msg_txt.get_content_charset('utf-8') - msg['body'] = msg['body'].decode(encoding).encode('utf-8') res_id = False if msg.get('references', False): -- cgit v1.2.3 From bddffbe8e69666d550f872e815975593c0a33ef0 Mon Sep 17 00:00:00 2001 From: Stephane Wirtel Date: Thu, 6 May 2010 18:41:47 +0200 Subject: [FIX] fetchmail: Decode correcly the body and the subject of the emails (ter) bzr revid: stephane@openerp.com-20100506164147-vpfqlk3ppvqqkd9n --- addons/fetchmail/fetchmail.py | 1 + 1 file changed, 1 insertion(+) (limited to 'addons/fetchmail/fetchmail.py') diff --git a/addons/fetchmail/fetchmail.py b/addons/fetchmail/fetchmail.py index 564c1c11e54..d8ae92654e3 100644 --- a/addons/fetchmail/fetchmail.py +++ b/addons/fetchmail/fetchmail.py @@ -195,6 +195,7 @@ class email_server(osv.osv): msg['message-id'] = message_id def _decode_header(txt): + txt = txt.replace('\r', '') return ' '.join(map(lambda (x, y): unicode(x, y or 'ascii'), decode_header(txt))) if 'Subject' in fields: -- cgit v1.2.3