Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11212 -- Stopped using quoted-printable encoding for mails wit…

…h non-ASCII characters but rely on 8bit encoding instead. Thanks, phr, gisle and Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16178 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2abd7af4ddd4d4cc83d2e3a52da1c76b10cd5a31 1 parent fe96214
Jannis Leidel authored
2  django/core/mail/message.py
@@ -21,7 +21,7 @@
21 21
 
22 22
 # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
23 23
 # some spam filters.
24  
-Charset.add_charset('utf-8', Charset.SHORTEST, Charset.QP, 'utf-8')
  24
+Charset.add_charset('utf-8', Charset.SHORTEST, None, 'utf-8')
25 25
 
26 26
 # Default MIME type to use on attachments (if it is not explicitly given
27 27
 # and cannot be guessed).
30  tests/regressiontests/mail/tests.py
@@ -120,7 +120,7 @@ def test_message_header_overrides(self):
120 120
         """
121 121
         headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
122 122
         email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers)
123  
-        self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent')
  123
+        self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent')
124 124
 
125 125
     def test_from_header(self):
126 126
         """
@@ -294,6 +294,30 @@ def test_dont_mangle_from_in_body(self):
294 294
         email = EmailMessage('Subject', 'From the future', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
295 295
         self.assertFalse('>From the future' in email.message().as_string())
296 296
 
  297
+    def test_dont_base64_encode(self):
  298
+        # Ticket #3472
  299
+        # Shouldn't use Base64 encoding at all
  300
+        msg = EmailMessage('Subject', 'UTF-8 encoded body', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
  301
+        self.assertFalse('Content-Transfer-Encoding: base64' in msg.message().as_string())
  302
+
  303
+        # Ticket #11212
  304
+        # Shouldn't use quoted printable, should detect it can represent content with 7 bit data
  305
+        msg = EmailMessage('Subject', 'Body with only ASCII characters.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
  306
+        s = msg.message().as_string()
  307
+        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
  308
+        self.assertTrue('Content-Transfer-Encoding: 7bit' in s)
  309
+
  310
+        # Shouldn't use quoted printable, should detect it can represent content with 8 bit data
  311
+        msg = EmailMessage('Subject', 'Body with latin characters: àáä.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
  312
+        s = msg.message().as_string()
  313
+        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
  314
+        self.assertTrue('Content-Transfer-Encoding: 8bit' in s)
  315
+
  316
+        msg = EmailMessage('Subject', u'Body with non latin characters: А Б В Г Д Е Ж Ѕ З И І К Л М Н О П.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
  317
+        s = msg.message().as_string()
  318
+        self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
  319
+        self.assertTrue('Content-Transfer-Encoding: 8bit' in s)
  320
+
297 321
 
298 322
 class BaseEmailBackendTests(object):
299 323
     email_backend = None
@@ -415,7 +439,7 @@ def test_message_cc_header(self):
415 439
         email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com'])
416 440
         mail.get_connection().send_messages([email])
417 441
         message = self.get_the_message()
418  
-        self.assertStartsWith(message.as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ')
  442
+        self.assertStartsWith(message.as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ')
419 443
 
420 444
     def test_idn_send(self):
421 445
         """
@@ -553,7 +577,7 @@ def test_console_stream_kwarg(self):
553 577
         s = StringIO()
554 578
         connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s)
555 579
         send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
556  
-        self.assertTrue(s.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: '))
  580
+        self.assertTrue(s.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: '))
557 581
 
558 582
 
559 583
 class FakeSMTPServer(smtpd.SMTPServer, threading.Thread):

0 notes on commit 2abd7af

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