Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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; 'output': 0.04; 'attribute': 0.05; 'none:': 0.05; 'try:': 0.07; 'base64': 0.09; 'bytes,': 0.09; 'charset': 0.09; 'encodes': 0.09; 'given,': 0.09; 'inherited': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'useless': 0.09; 'bug': 0.10; '3.3,': 0.16; '3.3.': 0.16; '8-bit': 0.16; 'adam': 0.16; 'lying.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'appears': 0.18; 'assumes': 0.22; 'skip:_ 20': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:m 30': 0.26; 'skip:1 30': 0.27; 'fixed': 0.28; 'header:X-Complaints-To:1': 0.28; 'question:': 0.29; "skip:' 10": 0.30; 'skip:8 10': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'text': 0.34; 'pm,': 0.35; 'received:org': 0.36; 'except': 0.36; 'explain': 0.36; 'method': 0.36; 'test': 0.36; 'should': 0.36; 'available.': 0.37; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'side': 0.61; 'provide': 0.62; 'due': 0.66; 'discover': 0.72; 'fact.': 0.84; 'received:fios.verizon.net': 0.84; 'text/html;': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: stmplib MIMEText charset weirdness Date: Tue, 26 Feb 2013 14:46:14 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 In-Reply-To: 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361907991 news.xs4all.nl 6971 [2001:888:2000:d::a6]:52272 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40000 On 2/25/2013 11:00 PM, Adam W. wrote: > Can someone explain to me why I can't set the charset after the fact. Email was revised to v.6 for 3.3, so the immediate answer to both your=20 why questions is 'because email was not revised yet'. > text =3D MIMEText('=E2=9D=A4=C2=A5'.encode('utf-8'), 'html') In 3.3 this fails immediately with AttributeError: 'bytes' object has no attribute 'encode' because when _charset is not given, MIMEText.__init__ test encodes to=20 discover what it should be if _charset is None: try: _text.encode('us-ascii') _charset =3D 'us-ascii' except UnicodeEncodeError: _charset =3D 'utf-8' > text =3D MIMEText('=E2=9D=A4=C2=A5'.encode('utf-8'), 'html', 'utf-8') If one provides bytes, one must provide the charset and MIMEText assumes = you are not lying. > text.as_string() > Content-Type: text/html; charset=3D"utf-8" > MIME-Version: 1.0 > Content-Transfer-Encoding: base64 > > 4p2kwqU=3D > Side question: > text =3D MIMEText('=E2=9D=A4=C2=A5', 'html') > text.set_charset('utf-8') This is redundant here. This method is inherited from Message and=20 appears pretty useless for the subclass. > text.as_string() > 'MIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nContent-Type: > text/html;charset=3D"utf-8"\n\n=E2=9D=A4=C2=A5' > > Why is it now 8-bit encoding? Bug fixed in 3.3. Output now same as above. Use 3.3 for email unless you = cannot due to other dependencies not yet being available. --=20 Terry Jan Reedy