From 6a7c0525f55bacced5512b04f4bbf9554e9e37eb Mon Sep 17 00:00:00 2001 From: rafaelol Date: Thu, 7 Jan 2016 11:47:29 -0200 Subject: [PATCH 1/2] Fix bug on Mail.send() when text or input are Unicode On PR #964 @matclab forced the encoding of both subject and text variables to unicode. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After merging it, matclab realized that when we send Unicode text to the method it raises an exception and asked if he should change the commit. Unfortunately this thing was kept untouched. This problem exists because we previously encode the unicode variables to utf-8 (for instance here https://github.com/web2py/web2py/blob/master/gluon/tools.py#L478-L481) and then force again to unicode. This piece of code shows what happens: ``` >>> a = u'áéí' >>> a u'\xe1\xe9\xed' >>> b = a.encode('utf-8') >>> b '\xc3\xa1\xc3\xa9\xc3\xad' >>> unicode(a) u'\xe1\xe9\xed' >>> unicode(b) Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) ``` If we force to str, just like @matclab suggested, we solve this issue. --- gluon/tools.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gluon/tools.py b/gluon/tools.py index f0a09850..3eee9d8a 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -777,16 +777,16 @@ class Mail(object): if attachments: result = mail.send_mail( sender=sender, to=origTo, - subject=unicode(subject), body=unicode(text), html=html, + subject=str(subject), body=str(text), html=html, attachments=attachments, **xcc) elif html and (not raw): result = mail.send_mail( sender=sender, to=origTo, - subject=unicode(subject), body=unicode(text), html=html, **xcc) + subject=str(subject), body=str(text), html=html, **xcc) else: result = mail.send_mail( sender=sender, to=origTo, - subject=unicode(subject), body=unicode(text), **xcc) + subject=str(subject), body=str(text), **xcc) else: smtp_args = self.settings.server.split(':') kwargs = dict(timeout=self.settings.timeout) From ba2cb811be102ea83f9c254d475a05405064d0f8 Mon Sep 17 00:00:00 2001 From: rafaelol Date: Thu, 7 Jan 2016 14:59:58 -0200 Subject: [PATCH 2/2] Changes encoding of text and subject on Mail.send() On the previous commit we changed text and subject from unicode to str. After a better solution from @cassiobotaro, we're using unicode again, selecting the encoding as the one passed via encoding parameter. --- gluon/tools.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gluon/tools.py b/gluon/tools.py index 3eee9d8a..63281234 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -777,16 +777,16 @@ class Mail(object): if attachments: result = mail.send_mail( sender=sender, to=origTo, - subject=str(subject), body=str(text), html=html, + subject=unicode(subject, encoding), body=unicode(text, encoding), html=html, attachments=attachments, **xcc) elif html and (not raw): result = mail.send_mail( sender=sender, to=origTo, - subject=str(subject), body=str(text), html=html, **xcc) + subject=unicode(subject, encoding), body=unicode(text, encoding), html=html, **xcc) else: result = mail.send_mail( sender=sender, to=origTo, - subject=str(subject), body=str(text), **xcc) + subject=unicode(subject, encoding), body=unicode(text, encoding), **xcc) else: smtp_args = self.settings.server.split(':') kwargs = dict(timeout=self.settings.timeout)