New submission from Shay Rojansky <r...@roji.org>:

This may be my misunderstanding of the correct behavior, thanks in advance for 
your patience...

The issue happens when calling set_charset (or set_payload charset) after a 
Message has already been created and contains a Content-Transfer-Encoding 
header. Here's an example:

>>> from email.mime.text import MIMEText
>>> 
>>> part = MIMEText('Some stuff aéàçça', 'plain', 'utf-8')
>>> print part
>From nobody Mon Feb 14 19:57:17 2011
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

U29tZSBzdHVmZiBhw6nDoMOnw6dh

>>> part.set_payload('Other stuff aéàçça', 'utf-8')
>>> print part
>From nobody Mon Feb 14 19:57:25 2011
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

Other stuff aéàçça
>>> 
>>> del part['Content-Transfer-Encoding']
>>> part.set_payload('Still some other stuff aéàçça', 'utf-8')
>>> print part
>From nobody Mon Feb 14 19:57:40 2011
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

U3RpbGwgc29tZSBvdGhlciBzdHVmZiBhw6nDoMOnw6dh

First the text part is created with charset UTF-8, a dump shows a 
properly-encoded base64 UTF-8 part.

Then an attempt is made to modify the payload. The set_charset documentation 
clearly states that the message will be properly encoded/converted, but we get 
a malformed part with Content-Transfer-Endogin=base64 but without a 
base64-encoded payload.

Finally, as a workaround, I delete the Content-Transfer-Encoding header and try 
again, at which point the new payload is properly encoded.

Again, I'm sure there are reasons for this behavior, which nevertheless seems 
like a bug to me (shouldn't set_charset perform base64 and change the 
Content-Transfer-Encoding if necessary regardless of previous headers?). Maybe 
a documentation update would help people with this.

Thank you very much!

----------
components: Library (Lib)
messages: 128573
nosy: Shay.Rojansky
priority: normal
severity: normal
status: open
title: email.message.Message set_charset does not encode properly?
type: behavior
versions: Python 2.6, Python 3.1

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11216>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to