Ticket #14301: idn_smtp.diff

File idn_smtp.diff, 2.3 KB (added by Claude Paroz, 14 years ago)

IDN encode in smtp backend

  • django/core/mail/backends/smtp.py

    diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py
    index 63efe43..b2370fc 100644
    a b class EmailBackend(BaseEmailBackend):  
    9595        """A helper method that does the actual sending."""
    9696        if not email_message.recipients():
    9797            return False
     98        from_email = _sanitize(email_message.from_email)
     99        recipients = map(_sanitize, email_message.recipients())
    98100        try:
    99             self.connection.sendmail(email_message.from_email,
    100                     email_message.recipients(),
     101            self.connection.sendmail(from_email, recipients,
    101102                    email_message.message().as_string())
    102103        except:
    103104            if not self.fail_silently:
    104105                raise
    105106            return False
    106107        return True
     108
     109def _sanitize(email):
     110    name, domain = email.split('@', 1)
     111    email = '@'.join([name, domain.encode('idna')])
     112    return email
     113
  • tests/regressiontests/mail/tests.py

    diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
    index a84417e..ec0e3be 100644
    a b class MailTests(TestCase):  
    378378        self.assertEqual(message.from_email, from_email)
    379379        self.assertEqual(message.to, [to_email])
    380380        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=?='))
     381
     382    def test_idn_smtp_send(self):
     383        import smtplib
     384        smtplib.SMTP = MockSMTP
     385        from_email = u'fröm@öäü.com'
     386        to_email = u'tö@öäü.com'
     387        connection = mail.get_connection('django.core.mail.backends.smtp.EmailBackend')
     388        self.assertTrue(send_mail('Subject', 'Content', from_email, [to_email], connection=connection))
     389
     390class MockSMTP(object):
     391    def __init__(self, host='', port=0, local_hostname=None,
     392                 timeout=1):
     393        pass
     394
     395    def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
     396                 rcpt_options=[]):
     397        for addr in to_addrs:
     398            str(addr.split('@', 1)[-1])
     399        return {}
     400
     401    def quit(self):
     402        return 0
Back to Top