X-Received: by 10.224.171.72 with SMTP id g8mr4495157qaz.7.1375370434695; Thu, 01 Aug 2013 08:20:34 -0700 (PDT) X-Received: by 10.50.77.78 with SMTP id q14mr146724igw.2.1375370434640; Thu, 01 Aug 2013 08:20:34 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!f7no409678qan.0!news-out.google.com!he10ni0qab.0!nntp.google.com!fx3no431463qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Thu, 1 Aug 2013 08:20:34 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=65.183.81.59; posting-account=h7ZmAgoAAAChnNHfIDF1M6YGHuonryl5 NNTP-Posting-Host: 65.183.81.59 References: <36aa5535-a821-4d7e-8414-1e40820705e4@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> Subject: Re: email 8bit encoding From: rurpy@yahoo.com Injection-Date: Thu, 01 Aug 2013 15:20:34 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.python:51739 On 07/29/2013 02:52 PM, Antoon Pardon wrote: > Op 29-07-13 01:41, rurpy@yahoo.com schreef: >> How, using Python-3.3's email module, do I "flatten" (I think >> that's the right term) a Message object to get utf-8 encoded >> body with the headers: >> Content-Type: text/plain; charset=UTF-8 >> Content-Transfer-Encoding: 8bit >> when the message payload was set to a python (unicode) string? >> > > I am just trying out some things for my self on python 3.2 so > be sure to test this out but you could try the following. > > msg.set_charset('utf-8') > msg['Content-Transfer-Encoding'] = '8bit' You can do that but the problem occurs when you call email.generator.flatten (or it is called on your behalf by somthing like smtplib.send_message) with such a message. flatten always assumes a 7bit encoding and uses the ascii codec to encode the message resulting in a UnicodeEncode exception when it hits an 8 bit character. So gymnastics like W. Trevor King implemented are necessary.