[Baypiggies] Failing at generating quoted-printable-encoded email

Asheesh Laroia asheesh at asheesh.org
Fri Sep 26 00:27:46 CEST 2008


Some quick background: I'm writing a(n open-source) app that generates 
emails.  There are (at least) two ways to encode characters not generally 
fit for email: base64 and quoted-printable.

It's easy to get proper base64-encoded output of email.mime.text:

 	>>> mt = email.mime.text.MIMEText('Ta mère', 'plain', 'utf-8')
 	>>> 'Content-Transfer-Encoding: base64' in mt.as_string()
 	True
 	>>> mt.as_string().split('\n')[-2]
 	'VGEgbcOocmU='

There we go, all nice and base64'd.

I can *not* figure out how to get quoted-printable-encoding.  I found 
http://docs.python.org/lib/module-email.encoders.html , so I thought great 
- I'll just encode my MIMEText object:

 	>>> email.encoders.encode_quopri(mt)
 	>>> 'Content-Transfer-Encoding: quoted-printable' in mt.as_string()
 	True

Great!  Except it's actually double-encoded, and the headers admit to as 
much:

 	>>> 'Content-Transfer-Encoding: base64' in mt.as_string()
 	True
 	>>> mt.as_string().split('\n')[-2]
 	'VGEgbcOocmU=3D'

It should look like:

 	>>> quopri.encodestring('ta mère')
 	'ta m=C3=A8re'

Um, how do I get these modules to only encode the mail *once*, as 
quoted-printable?

This seems like a bug in the module - the double-encoding - it could 
easily be me walking down the wrong path in a module I'm not an expert in 
using.  http://www.google.com/codesearch?q=encode_quopri&hl=en&btnG=Search+Code 
refers to a now-removed _encoding= parameter.

(I want to use quoted-printable because it makes writing my tests 
straightforward as mock tests with Ian Bicking's MiniMock 
<http://pypi.python.org/pypi/MiniMock/> and doctests + doctest.ELLIPSIS. 
Plus, it's friendlier to people like my friend Kragen who read their email 
in /usr/bin/less!)

-- Asheesh.

-- 
It shall be unlawful for any suspicious person to be within the municipality.
 		-- Local ordinance, Euclid Ohio


More information about the Baypiggies mailing list