changeset 8121:7aff9d527bf1

lib: fix mail address encodings and add test coverage Now it also works on Py3 ... apparently in more cases and more correctly than before.
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 28 Dec 2019 17:36:09 +0100
parents 58b6e4cd6fe9
children bf009cb3a470
files kallithea/lib/rcmail/response.py kallithea/tests/other/test_mail.py
diffstat 2 files changed, 11 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/rcmail/response.py	Sat Dec 28 13:38:22 2019 +0100
+++ b/kallithea/lib/rcmail/response.py	Sat Dec 28 17:36:09 2019 +0100
@@ -346,13 +346,13 @@
 
     for k in mail.keys():
         if k in ADDRESS_HEADERS_WHITELIST:
-            out[k.encode('ascii')] = header_to_mime_encoding(
+            out[k] = header_to_mime_encoding(
                                          mail[k],
                                          not_email=False,
                                          separator=separator
                                      )
         else:
-            out[k.encode('ascii')] = header_to_mime_encoding(
+            out[k] = header_to_mime_encoding(
                                          mail[k],
                                          not_email=True
                                     )
@@ -443,12 +443,12 @@
     check different, then change this.
     """
     try:
-        return value.encode("ascii")
-    except UnicodeEncodeError:
+        value.encode("ascii")
+        return value
+    except UnicodeError:
         if not not_email and VALUE_IS_EMAIL_ADDRESS(value):
             # this could have an email address, make sure we don't screw it up
             name, address = parseaddr(value)
-            return '"%s" <%s>' % (
-                encoder.header_encode(name.encode("utf-8")), address)
+            return '"%s" <%s>' % (encoder.header_encode(name), address)
 
-        return encoder.header_encode(value.encode("utf-8"))
+        return encoder.header_encode(value)
--- a/kallithea/tests/other/test_mail.py	Sat Dec 28 13:38:22 2019 +0100
+++ b/kallithea/tests/other/test_mail.py	Sat Dec 28 17:36:09 2019 +0100
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
 import mock
 
 import kallithea
@@ -144,9 +146,9 @@
 
     def test_send_mail_with_author_full_mail_from(self):
         mailserver = 'smtp.mailserver.org'
-        recipients = ['rcpt1', 'rcpt2']
+        recipients = ['ræcpt1', 'receptor2 <rcpt2@example.com>', 'tæst@example.com', 'Tæst <test@example.com>']
         envelope_addr = 'noreply@mailserver.org'
-        envelope_from = 'Some Name <%s>' % envelope_addr
+        envelope_from = 'Söme Næme <%s>' % envelope_addr
         subject = 'subject'
         body = 'body'
         html_body = 'html_body'