stmplib MIMEText charset weirdness

Adam W. AWasilenko at gmail.com
Tue Feb 26 10:29:19 EST 2013


On Tuesday, February 26, 2013 2:10:28 AM UTC-5, Steven D'Aprano wrote:
> On Mon, 25 Feb 2013 20:00:24 -0800, Adam W. wrote:
> 
> The documentation for MIMEText is rather terse, but it implies that the 
> 
> parameter given should be a string, not bytes:
> 
> 
> 
> http://docs.python.org/3.2/library/email.mime#email.mime.text.MIMEText
> 
> 
> 
> If I provide a string, it seems to work fine:
> 
> 


Ok, working under the assumption you need to provide it a string, it still leaves the question why adding the header after the fact (to a string input) does not produce the same result as declaring the encoding type inline.

 
> 
> > As opposed to:
> 
> >
> 
> >>>> text = MIMEText('❤¥'.encode('utf-8'), 'html', 'utf-8')
> 
> >>>> text.as_string()
> 
> > 'Content-Type: text/html; charset="utf-8"\nMIME-Version:
> 
> > 1.0\nContent-Transfer-Encoding: base64\n\n4p2kwqU=\n'
> 
> 
> 
> 
> 
> My wild guess is that it is an accident (possibly a bug) that the above 
> 
> works at all. I think it shouldn't; MIMEText is expecting a string, and 
> 
> you provide a bytes object. The documentation for the email package 
> 
> states:
> 
> 
> 
> 
> 
> [quote]
> 
> Here are the major differences between email version 5.0 and version 4:
> 
> 
> 
>     All operations are on unicode strings. Text inputs must be strings, 
> 
> text outputs are strings. Outputs are limited to the ASCII character set 
> 
> and so can be encoded to ASCII for transmission. Inputs are also limited 
> 
> to ASCII; this is an acknowledged limitation of email 5.0 and means it 
> 
> can only be used to parse email that is 7bit clean.
> 
> [end quote]
> 
> 
> 
> http://docs.python.org/3.2/library/email.html
> 

I find this limitation hard to believe, why bother with encoding flags if it can only ever accept ASCII anyway?

The reason this issue came up was because I was adding the header after like in my examples and it wasn't working, so I Google'd around and found this Stackoverflow: http://stackoverflow.com/questions/10295530/how-to-set-a-charset-in-email-using-smtplib-in-python-2-7

Which seemed to be doing exactly what I wanted, with the only difference is the inline deceleration of utf-8, with that change it started working as desired...



More information about the Python-list mailing list