Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14301 -- Handle email validation gracefully with email address…

…es containing non-ASCII characters. Thanks, Andi Albrecht.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14216 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 36f2f7ee7c806824b28bdf669296cc0730fe6100 1 parent 767cf95
Jannis Leidel authored October 14, 2010
6  django/core/mail/message.py
@@ -67,7 +67,11 @@ def forbid_multi_line_headers(name, val, encoding):
67 67
             result = []
68 68
             for nm, addr in getaddresses((val,)):
69 69
                 nm = str(Header(nm.encode(encoding), encoding))
70  
-                result.append(formataddr((nm, str(addr))))
  70
+                try:
  71
+                    addr = addr.encode('ascii')
  72
+                except UnicodeEncodeError:  # IDN
  73
+                    addr = str(Header(addr.encode(encoding), encoding))
  74
+                result.append(formataddr((nm, addr)))
71 75
             val = ', '.join(result)
72 76
         else:
73 77
             val = Header(val.encode(encoding), encoding)
15  tests/regressiontests/mail/tests.py
@@ -363,3 +363,18 @@ def test_mail_prefix(self):
363 363
 
364 364
         settings.ADMINS = old_admins
365 365
         settings.MANAGERS = old_managers
  366
+
  367
+    def test_idn_validation(self):
  368
+        """Test internationalized email adresses"""
  369
+        # Regression for #14301.
  370
+        mail.outbox = []
  371
+        from_email = u'fröm@öäü.com'
  372
+        to_email = u'tö@öäü.com'
  373
+        connection = mail.get_connection('django.core.mail.backends.locmem.EmailBackend')
  374
+        send_mail('Subject', 'Content', from_email, [to_email], connection=connection)
  375
+        self.assertEqual(len(mail.outbox), 1)
  376
+        message = mail.outbox[0]
  377
+        self.assertEqual(message.subject, 'Subject')
  378
+        self.assertEqual(message.from_email, from_email)
  379
+        self.assertEqual(message.to, [to_email])
  380
+        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=?='))

0 notes on commit 36f2f7e

Please sign in to comment.
Something went wrong with that request. Please try again.