Skip to content
Browse files

First fix for long email address encoding

  • Loading branch information...
1 parent bb66bb5 commit 95cedcfe2bbf52e132c6d504c1bb44ea66ed0200 Dan Fairs committed Apr 23, 2012
Showing with 33 additions and 5 deletions.
  1. +9 −2 django_mailer/engine.py
  2. +2 −1 django_mailer/tests/__init__.py
  3. +22 −2 django_mailer/tests/engine.py
View
11 django_mailer/engine.py
@@ -24,6 +24,13 @@
logger = logging.getLogger('django_mailer.engine')
+def _fallback_encode(text, encoding='idna', fallback='utf8'):
+ try:
+ return text.encode(encoding)
+ except UnicodeError:
+ return text.encode(fallback)
+
+
def _message_queue(block_size):
"""
A generator which iterates queued messages in blocks so that new
@@ -170,8 +177,8 @@ def send_queued_message(queued_message, smtp_connection=None, blacklist=None,
result = constants.RESULT_SKIPPED
else:
try:
- from_address = message.from_address.encode('idna')
- to_address = message.to_address.encode('idna')
+ from_address = _fallback_encode(message.from_address)
+ to_address = _fallback_encode(message.to_address)
logger.info("Sending message to %s: %s" %
(to_address,
message.subject.encode("utf-8")))
View
3 django_mailer/tests/__init__.py
@@ -1,3 +1,4 @@
from django_mailer.tests.commands import TestCommands
from django_mailer.tests.engine import LockTest #COULD DROP THIS TEST
-from django_mailer.tests.backend import TestBackend
+from django_mailer.tests.engine import AddressTestCase
+from django_mailer.tests.backend import TestBackend
View
24 django_mailer/tests/engine.py
@@ -1,6 +1,9 @@
+from django.core import mail
+from django.core.management import call_command
from django.test import TestCase
from django_mailer import engine, settings
from django_mailer.lockfile import FileLock
+from django_mailer.tests.base import MailerTestCase
from StringIO import StringIO
import logging
import time
@@ -13,7 +16,7 @@ class LockTest(TestCase):
"""
def setUp(self):
- # Create somewhere to store the log debug output.
+ # Create somewhere to store the log debug output.
self.output = StringIO()
# Create a log handler which can capture the log debug output.
self.handler = logging.StreamHandler(self.output)
@@ -23,7 +26,7 @@ def setUp(self):
# Add the log handler.
logger = logging.getLogger('django_mailer')
logger.addHandler(self.handler)
-
+
# Set the LOCK_WAIT_TIMEOUT to the default value.
self.original_timeout = settings.LOCK_WAIT_TIMEOUT
settings.LOCK_WAIT_TIMEOUT = 0
@@ -82,3 +85,20 @@ def fake_time():
'Lock already in place. Exiting.')
finally:
time.time = original_time
+
+
+class AddressTestCase(MailerTestCase):
+
+ def test_bad_idna(self):
+ # If idna encoding fails (for example, email address too long) then
+ # fall back to UTF8 and hope for the best.
+ to_email = ('ootofestivalrareatmosphericphenomenacontinuallyoccuring'
+ '@outoftheordinaryfestival.com')
+ from_email = 'me@me.com'
+ self.queue_message(from_email=from_email, recipient_list=[to_email])
+ call_command('send_mail', verbosity='0')
+ self.assertEqual(1, len(mail.outbox))
+ engine.send_all()
+ m = mail.outbox[0]
+ self.assertEqual(from_email.encode('utf8'), m.from_email)
+ self.assertEqual([to_email.encode('utf8')], m.to)

0 comments on commit 95cedcf

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