Thursday, March 29, 2012

EmailMessage in Django v1.4

In Django v1.4, creating email messages with the EmailMessage class now tries to default to 7-bit encoding unless there is a reason to use quoted-printable format. The reason? It appears that using ASCII text helps to prevent extra attention from spam filters.

https://code.djangoproject.com/attachment/ticket/11212/0001-Ticket-11212-default-to-7bit-email.patch

If you have any tests that attempt to compare the email-message against what you're sending, you'll need to update them too!

+        # Ticket #11212
+ # Shouldn't use quoted printable, should detect it can represent content with 7 bit data
+ msg = EmailMessage('Subject', 'Body with only ASCII characters.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
+ s = msg.message().as_string()
+ self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
+ self.assertTrue('Content-Transfer-Encoding: 7bit' in s)
+
+ # Shouldn't use quoted printable, should detect it can represent content with 8 bit data
+ msg = EmailMessage('Subject', 'Body with latin characters: àáä.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
+ s = msg.message().as_string()
+ self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
+ self.assertTrue('Content-Transfer-Encoding: 8bit' in s)
+
+ msg = EmailMessage('Subject', u'Body with non latin characters: А Б В Г Д Е Ж Ѕ З И І К Л М Н О П.', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
+ s = msg.message().as_string()
+ self.assertFalse('Content-Transfer-Encoding: quoted-printable' in s)
+ self.assertTrue('Content-Transfer-Encoding: 8bit' in s)

1 comment:

  1. How do I manually specify "Content-Transfer-Encoding: quoted-printable" if I use EmailMultiAlternatives ?

    ReplyDelete