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

Matt Good matt at matt-good.net
Fri Sep 26 02:23:24 CEST 2008

On Sep 25, 2008, at 3:27 PM, Asheesh Laroia wrote:

> 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.

There might be a better way, but this is the hack that Trac uses to  
get qp working:

import email.mime.text
import email.charset

charset = email.charset.Charset('utf-8')
charset.header_encoding = email.charset.QP
charset.body_encoding = email.charset.QP

msg = email.mime.text.MIMEText('foo', 'plain')
# Message class computes the wrong type from MIMEText constructor,
# which does not take a Charset object as initializer. Reset the
# encoding type to force a new, valid evaluation
del msg['Content-Transfer-Encoding']

-- Matt

More information about the Baypiggies mailing list