From 36f2f7ee7c806824b28bdf669296cc0730fe6100 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Thu, 14 Oct 2010 18:37:05 +0000 Subject: [PATCH] Fixed #14301 -- Handle email validation gracefully with email addresses containing non-ASCII characters. Thanks, Andi Albrecht. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14216 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/mail/message.py | 6 +++++- tests/regressiontests/mail/tests.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index a0cb09f7b8..2311102fd0 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -67,7 +67,11 @@ def forbid_multi_line_headers(name, val, encoding): result = [] for nm, addr in getaddresses((val,)): nm = str(Header(nm.encode(encoding), encoding)) - result.append(formataddr((nm, str(addr)))) + try: + addr = addr.encode('ascii') + except UnicodeEncodeError: # IDN + addr = str(Header(addr.encode(encoding), encoding)) + result.append(formataddr((nm, addr))) val = ', '.join(result) else: val = Header(val.encode(encoding), encoding) diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index d2f7562d8f..a84417e21b 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -363,3 +363,18 @@ class MailTests(TestCase): settings.ADMINS = old_admins settings.MANAGERS = old_managers + + def test_idn_validation(self): + """Test internationalized email adresses""" + # Regression for #14301. + mail.outbox = [] + from_email = u'fröm@öäü.com' + to_email = u'tö@öäü.com' + connection = mail.get_connection('django.core.mail.backends.locmem.EmailBackend') + send_mail('Subject', 'Content', from_email, [to_email], connection=connection) + self.assertEqual(len(mail.outbox), 1) + message = mail.outbox[0] + self.assertEqual(message.subject, 'Subject') + self.assertEqual(message.from_email, from_email) + self.assertEqual(message.to, [to_email]) + self.assertTrue(message.message().as_string().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: =?utf-8?b?ZnLDtm1Aw7bDpMO8LmNvbQ==?=\nTo: =?utf-8?b?dMO2QMO2w6TDvC5jb20=?='))