Debugging utf-8 Email In Django And Python

16:40 Tuesday, 22 December 2009

In case anyone else is wondering why their utf-8 email is not coming out as it should when using Django…

Non-ascii character sets are a major pain when writing code to send email. They’re not impossible by a long way but they do take some digging to figure out why your nicely formated utf-8 Chinese is coming out in garbled characters, tracing code, examing email headers… Oh the joy. So it does not help when your framework of choice (Django) decides it knows that you really want a certain type of text encoding, even if you are writing your own email routines, with a global piece of code such as the below.

 # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
 # some spam filters.
 Charset.add_charset('utf-8', Charset.SHORTEST, Charset.QP, 'utf-8')

What this baby does is change the default behaviour of the Python standard library when handling utf-8 email. And induces substantial amounts of hair pulling and teeth grinding when trying to figure out why your code is not doing what it should.

While the goal of not falling into a spam trap is a good one. Surely the coding decision should be up to the user.

I’ve submitted a Django ticket #12422 for this.

Tags