Peter Otten wrote: > Chris Withers wrote: > >> At worst, and most likely based on my past experience of (c)StringIO >> being used to accumulate output, it won't make a jot of difference... > > What past experience? > >>>> StringIO.StringIO().write(unichr(128)) >>>> cStringIO.StringIO().write(unichr(128)) > Traceback (most recent call last): > File "<stdin>", line 1, in ? > UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position > 0: ordinal not in range(128)
Okay, more out of desperation than anything else, lets try this: from email.Charset import Charset,QP from email.MIMEText import MIMEText from StringIO import StringIO from email import Generator,Message Generator.StringIO = Message.StringIO = StringIO charset = Charset('utf-8') charset.body_encoding = QP msg = MIMEText(u'Some text with chars that need encoding: \xa3','plain') msg.set_charset(charset) print repr(msg.as_string()) u'MIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nContent-Type: text/plain; charset="utf-8"\n\nSome text with chars that need encoding: \xa3' Yay! No unicode error, but also no use: File "c:\python24\lib\smtplib.py", line 692, in sendmail (code,resp) = self.data(msg) File "c:\python24\lib\smtplib.py", line 489, in data self.send(q) File "c:\python24\lib\smtplib.py", line 316, in send self.sock.sendall(str) File "<string>", line 1, in sendall UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 297: ordinal not in range(128) The other variant I've tried is: from email.Charset import Charset,QP from email.MIMEText import MIMEText charset = Charset('utf-8') charset.body_encoding = QP msg = MIMEText('','plain',) msg.set_charset(charset) msg.set_payload(charset.body_encode(u'Some text with chars that need encoding: \xa3')) print msg.as_string() Which is sort of okay: MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" Some text with chars that need encoding: =A3 ...except it gets the transfer encoding wrong, which means Thunderbird shows =A3 instead of the pound sign that it should :-( ...this is down to a pretty lame bit of code in Encoders.py which basically checks for a unicode error *sigh* Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list