Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'encoding': 0.05; 'messages.': 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; 'def': 0.12; "'''": 0.16; 'character.': 0.16; 'codec': 0.16; 'hits': 0.16; 'how,': 0.16; 'multipart': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'module,': 0.24; 'necessary.': 0.24; 'skip:e 30': 0.24; 'server.': 0.24; '(or': 0.24; 'header:In-Reply-To:1': 0.27; '3.2': 0.31; 'assumes': 0.31; 'het': 0.31; 'occurs': 0.31; 'txt': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'implemented': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'message.': 0.35; 'problem': 0.35; 'test': 0.35; 'but': 0.35; 'smtp': 0.36; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'such': 0.63; 'email addr:yahoo.com': 0.64; 'our': 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: AqAEAPCg+1GGuA9G/2dsb2JhbABasDySIYEygxgBAQUyAUURCxgJFg8JAwIBAgEzEhADBgICh3oDD7EZiAeNJ4EpgU8Wg3cDlXqBZYYRhhaFJ4MZgW8 Date: Fri, 02 Aug 2013 14:12:04 +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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375445526 news.xs4all.nl 15954 [2001:888:2000:d::a6]:46511 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51798 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. This works too, but I don't know how usefull it is with multipart messages. import smtplib import os import io from email.mime.text import MIMEText from email.generator import BytesGenerator from email.generator import BytesGenerator class EncodeGenerator(BytesGenerator): def flatten(self, msg, unixfrom=False, linesep='\n'): self._encoding = str(msg.get_charset()) super().flatten(msg, unixfrom, linesep) def write(self, s): self._fp.write(s.encode(self._encoding)) def _encode(self, s): return s.encode(self._encoding) 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 test: %d' % os.getpid() msg['From'] = sender msg['To'] = recipient print(msg['Subject']) # Send the message via our own SMTP server. s = smtplib.SMTP('localhost') with io.BytesIO() as bytesmsg: g = EncodeGenerator(bytesmsg) g.flatten(msg, linesep='\r\n') flat_msg = bytesmsg.getvalue() print(flat_msg) s.sendmail(sender, [recipient], flat_msg) s.quit()