Code

Ticket #14301: bug14301.diff

File bug14301.diff, 1.9 KB (added by andialbrecht, 4 years ago)
Line 
1diff --git a/django/core/mail/message.py b/django/core/mail/message.py
2index a0cb09f..2311102 100644
3--- a/django/core/mail/message.py
4+++ b/django/core/mail/message.py
5@@ -67,7 +67,11 @@ def forbid_multi_line_headers(name, val, encoding):
6             result = []
7             for nm, addr in getaddresses((val,)):
8                 nm = str(Header(nm.encode(encoding), encoding))
9-                result.append(formataddr((nm, str(addr))))
10+                try:
11+                    addr = addr.encode('ascii')
12+                except UnicodeEncodeError:  # IDN
13+                    addr = str(Header(addr.encode(encoding), encoding))
14+                result.append(formataddr((nm, addr)))
15             val = ', '.join(result)
16         else:
17             val = Header(val.encode(encoding), encoding)
18diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
19index 29f63a5..c46f31b 100644
20--- a/tests/regressiontests/mail/tests.py
21+++ b/tests/regressiontests/mail/tests.py
22@@ -471,4 +471,23 @@ Content
23 >>> message.as_string()
24 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ...\nMessage-ID: <...>\n\nContent'
25 
26+# Allow IDN in send_mail (14301)
27+
28+>>> from_email = u'from@öäü.com'
29+>>> to_email = u'to@öäü.com'
30+>>> connection = mail.get_connection('django.core.mail.backends.console.EmailBackend')
31+>>> send_mail('Subject', 'Content', from_email, [to_email], connection=connection)
32+Content-Type: text/plain; charset="utf-8"
33+MIME-Version: 1.0
34+Content-Transfer-Encoding: quoted-printable
35+Subject: Subject
36+From: =?utf-8?b?ZnJvbUDDg8K2w4PCpMODwrwuY29t?=
37+To: =?utf-8?b?dG9Aw4PCtsODwqTDg8K8LmNvbQ==?=
38+Date: ...
39+Message-ID: ...
40+
41+Content
42+-------------------------------------------------------------------------------
43+1
44+
45 """