From bd7fd89ee715c3e24f0451466c93d9c2091a5e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Pio?= Date: Tue, 16 Dec 2014 21:11:12 -0200 Subject: Fix #195 and #198 special characters handling Added check to see if message body is base64, properly decode the body if they are Saving a draft with special characters now properly uses utf-8, that means it will send the body base64 encoded --- service/pixelated/adapter/mail.py | 10 +++++++--- service/test/unit/adapter/mail_test.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'service') diff --git a/service/pixelated/adapter/mail.py b/service/pixelated/adapter/mail.py index 946b921d..847773b7 100644 --- a/service/pixelated/adapter/mail.py +++ b/service/pixelated/adapter/mail.py @@ -25,6 +25,7 @@ import pixelated.support.date from email.MIMEMultipart import MIMEMultipart from pycryptopp.hash import sha256 import re +import base64 class Mail(object): @@ -71,7 +72,7 @@ class Mail(object): if 'content_type' in self.headers and 'charset' in self.headers['content_type']: return re.compile('.*charset=(.*)').match(self.headers['content_type']).group(1) else: - return 'us-ascii' + return 'utf-8' @property def raw(self): @@ -157,7 +158,7 @@ class InputMail(Mail): for part in self.body: mime_multipart.attach(MIMEText(part['raw'], part['content-type'])) else: - mime_multipart.attach(MIMEText(self.body, 'plain')) + mime_multipart.attach(MIMEText(self.body, 'plain', 'utf-8')) return mime_multipart def to_smtp_format(self): @@ -210,7 +211,10 @@ class PixelatedMail(Mail): body += '--' + self.boundary + '--' return body else: - return self.bdoc.content['raw'] + if self.parts and self.parts['alternatives'][0]['headers']['Content-Transfer-Encoding'] == 'base64': + return base64.b64decode(self.parts['alternatives'][0]['content']) + else: + return self.bdoc.content['raw'] @property def headers(self): diff --git a/service/test/unit/adapter/mail_test.py b/service/test/unit/adapter/mail_test.py index 10886a27..342f1e86 100644 --- a/service/test/unit/adapter/mail_test.py +++ b/service/test/unit/adapter/mail_test.py @@ -20,7 +20,7 @@ from pixelated.adapter.mail import PixelatedMail, InputMail from mockito import * from test.support import test_helper import dateutil.parser as dateparser - +import base64 class TestPixelatedMail(unittest.TestCase): def setUp(self): @@ -170,6 +170,17 @@ class TestPixelatedMail(unittest.TestCase): mail.raw + def test_that_body_understands_base64(self): + body = "bl\xe1" + encoded_body = base64.b64encode(body) + + fdoc, hdoc, bdoc = test_helper.leap_mail() + parts = {'alternatives': []} + parts['alternatives'].append({'content': encoded_body, 'headers': {'Content-Transfer-Encoding': 'base64'}}) + mail = PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self.querier, parts=parts) + + self.assertEquals(body, mail.body) + class InputMailTest(unittest.TestCase): mail_dict = lambda x: { 'body': 'Este \xe9 o corpo', @@ -222,7 +233,7 @@ class InputMailTest(unittest.TestCase): self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: bcc@pixelated.org, anotherbcc@pixelated.org\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nDate: date now\n") self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n") - self.assertRegexpMatches(mime_multipart.as_string(), "\nEste \xe9 o corpo") + self.assertRegexpMatches(mime_multipart.as_string(), base64.b64encode(self.mail_dict()['body'])) def test_smtp_format(self): InputMail.FROM_EMAIL_ADDRESS = 'pixelated@org' -- cgit v1.2.3