Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14301 -- Further refine changes made in r14216 to support non-…

…ASCII characters in email addresses. Thanks, Claude Peroz and Andi Albrecht.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15006 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d4ef8414957e8e5db43d396eeb77bac4cf654bf8 1 parent 673e6fc
Jannis Leidel authored December 21, 2010
10  django/core/mail/backends/smtp.py
@@ -91,13 +91,19 @@ def send_messages(self, email_messages):
91 91
             self._lock.release()
92 92
         return num_sent
93 93
 
  94
+    def _sanitize(self, email):
  95
+        name, domain = email.split('@', 1)
  96
+        email = '@'.join([name, domain.encode('idna')])
  97
+        return email
  98
+
94 99
     def _send(self, email_message):
95 100
         """A helper method that does the actual sending."""
96 101
         if not email_message.recipients():
97 102
             return False
  103
+        from_email = self._sanitize(email_message.from_email)
  104
+        recipients = map(self._sanitize, email_message.recipients())
98 105
         try:
99  
-            self.connection.sendmail(email_message.from_email,
100  
-                    email_message.recipients(),
  106
+            self.connection.sendmail(from_email, recipients,
101 107
                     email_message.message().as_string())
102 108
         except:
103 109
             if not self.fail_silently:
22  tests/regressiontests/mail/tests.py
@@ -408,3 +408,25 @@ def test_idn_validation(self):
408 408
         self.assertEqual(message.from_email, from_email)
409 409
         self.assertEqual(message.to, [to_email])
410 410
         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=?='))
  411
+
  412
+    def test_idn_smtp_send(self):
  413
+        import smtplib
  414
+        smtplib.SMTP = MockSMTP
  415
+        from_email = u'fröm@öäü.com'
  416
+        to_email = u'tö@öäü.com'
  417
+        connection = mail.get_connection('django.core.mail.backends.smtp.EmailBackend')
  418
+        self.assertTrue(send_mail('Subject', 'Content', from_email, [to_email], connection=connection))
  419
+
  420
+class MockSMTP(object):
  421
+    def __init__(self, host='', port=0, local_hostname=None,
  422
+                 timeout=1):
  423
+        pass
  424
+
  425
+    def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
  426
+                 rcpt_options=[]):
  427
+        for addr in to_addrs:
  428
+            str(addr.split('@', 1)[-1])
  429
+        return {}
  430
+
  431
+    def quit(self):
  432
+        return 0

0 notes on commit d4ef841

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