Chris Withers wrote: > Has no-one ever successfully generated a correctly formatted email with > email.MIMEText where the message includes non-ascii characters?!
I'm guessing not ;-) Well, I think I have a winner, but it required me to subclass MIMEText: from email.Charset import Charset,QP from email.MIMEText import MIMEText as OriginalMIMEText from email.MIMENonMultipart import MIMENonMultipart class MIMEText(OriginalMIMEText): def __init__(self, _text, _subtype='plain', _charset='us-ascii'): if isinstance(_charset,Charset): cs = _charset.input_charset else: cs = _charset if isinstance(_text,unicode): _text = _text.encode(charset.input_charset) MIMENonMultipart.__init__(self, 'text', _subtype, **{'charset': cs}) self.set_payload(_text, _charset) charset = Charset('utf-8') charset.body_encoding = QP txt = u'Some text with chars that need encoding:\xa3' msg = MIMEText(txt,'plain',charset) print msg.as_string() Which gives: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Some text with chars that need encoding:=C2=A3 It also works with non-QP charsets. The reason the subclass is needed is because the MIMNonMultipart.__init__ cannot handle a charset which isn't a simple string. Since it's needed for that reason, it seems like the right place to encode any incoming unicode. So, by my count, there are two bugs: 1. email.MIMEText.MIMEText can't take a real Charset object to its __init__ method. 2. email.Message.Message.set_payload has no clue about unicode. Does that sounds fair? If so, should I open SF issues for them? cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list