Author: jezdez
Date: 2010-10-14 13:38:43 -0500 (Thu, 14 Oct 2010)
New Revision: 14217

Modified:
   django/branches/releases/1.2.X/django/core/mail/message.py
   django/branches/releases/1.2.X/tests/regressiontests/mail/tests.py
Log:
[1.2.X] Fixed #14301 -- Handle email validation gracefully with email addresses 
containing non-ASCII characters. Thanks, Andi Albrecht.

Backport from trunk (r14216).

Modified: django/branches/releases/1.2.X/django/core/mail/message.py
===================================================================
--- django/branches/releases/1.2.X/django/core/mail/message.py  2010-10-14 
18:37:05 UTC (rev 14216)
+++ django/branches/releases/1.2.X/django/core/mail/message.py  2010-10-14 
18:38:43 UTC (rev 14217)
@@ -67,7 +67,11 @@
             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)

Modified: django/branches/releases/1.2.X/tests/regressiontests/mail/tests.py
===================================================================
--- django/branches/releases/1.2.X/tests/regressiontests/mail/tests.py  
2010-10-14 18:37:05 UTC (rev 14216)
+++ django/branches/releases/1.2.X/tests/regressiontests/mail/tests.py  
2010-10-14 18:38:43 UTC (rev 14217)
@@ -336,3 +336,18 @@
 
         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...@öäü.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=?='))

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to