Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'encoding': 0.05; 'received:134': 0.05; 'encoded': 0.07; 'utf-8': 0.07; "'python": 0.09; 'ascii': 0.09; 'encode': 0.09; 'msg': 0.09; 'python': 0.11; '"..."': 0.16; "'''": 0.16; 'buggy': 0.16; 'character.': 0.16; 'codec': 0.16; 'hits': 0.16; 'how,': 0.16; 'us-ascii': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'module,': 0.24; 'necessary.': 0.24; '(or': 0.24; 'least': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'code': 0.31; '3.2': 0.31; 'assumes': 0.31; 'het': 0.31; 'occurs': 0.31; 'txt': 0.31; 'skip:m 30': 0.32; 'implemented': 0.33; 'could': 0.34; 'message.': 0.35; 'problem': 0.35; '(2)': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; "couldn't": 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'called': 0.40; 'skip:u 10': 0.60; 'is.': 0.60; 'such': 0.63; 'email addr:yahoo.com': 0.64; 'adres': 0.84; 'pardon': 0.84; 'trevor': 0.84; '8bit': 0.91 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqAEADSV+1GGuA9G/2dsb2JhbABasDySIYEygxgBAQUyAUURCxgJFg8JAwIBAgEzEhADBgICh3oDD7EOiAeNJ4EpgU8Wg3cDlXqBZYYRhhaFJ4MZgW8 Date: Fri, 02 Aug 2013 13:19:11 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: email 8bit encoding References: <36aa5535-a821-4d7e-8414-1e40820705e4@googlegroups.com> <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> In-Reply-To: <1672c141-7d90-477a-a62c-8e0e97bda9be@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375442359 news.xs4all.nl 15917 [2001:888:2000:d::a6]:51779 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51795 Op 01-08-13 17:20, rurpy@yahoo.com schreef: > 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. Well this works for me. I had a little look in the code and it seems buggy to me, at least the 3.2 version is. There is a _encode but (1) The bytes generator version is defined to allways use us-ascii as encoding and (2) I couldn't find it actually being called on a mesg part. ------------------------------------------------------------ import smtplib from email.mime.text import MIMEText txt = '''\ Het adres is Fréderic Boëven Frère Orbanstraat 17 ''' recipient = "..." sender = "..." msg = MIMEText(txt) msg.set_charset("utf-8") msg['Subject'] = 'Python email tets' msg['From'] = sender msg['To'] = recipient s = smtplib.SMTP('localhost') s.sendmail(sender, [recipient], msg.as_string().encode("utf-8")) s.quit()