[Mailman-Developers] modifying payload using same encoding and Content-Transfer-Encoding

Mark Sapiro mark at msapiro.net
Wed May 14 02:45:47 CEST 2014


On 05/13/2014 07:14 AM, Sylvain Viart wrote:
> Hi,
> 
> Thanks for your reply. Unfortunately it doesn't work. I don't know why.
> I did read the doc many times, without the expected result.


It works for me. The following withlist interaction based on a stripped
down version of what you originally posted shows it works.

I create a message with utf-8, base64 encoded content. I get its decoded
payload, delete the existing Content-Transfer-Encoding: header and set a
new payload (not actually different, but could be) with the charset from
the original message and the body is base64 encoded and the
Content-Transfer-Encoding: header is set appropriately. And the Python
email package's encoding for utf-8 is base64, so the encoding will be
base64 if you just include the utf-8 charset argument in the call to
set_payload().

This

>             del msg['content-transfer-encoding']
>             charset = Charset("utf-8")
>             charset.header_encoding = BASE64
>             charset.body_encoding = BASE64
>             msg.set_charset(charset)

All gets undone when you do this

>             msg.set_payload(new_content)


$ /var/MM/21/bin/withlist -i
No list name supplied.
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import email
>>> from Mailman.Message import Message
>>> msg = email.message_from_string("""From: me
... To: you
... Subject: test message
... Content-Transfer-Encoding: base64
... MIME-Version: 1.0
... Content-Type: text/html; charset="utf-8"
...
...
PGh0bWw+CjxoZWFkPgo8L2hlYWQ+Cjxib2R5PgpUaGlzIGlzIGEgdGVzdAo8L2JvZHk+CjwvaHRt
... bD4K
... """, Message)
>>> msg
>From nobody Tue May 13 17:30:12 2014
From: me
To: you
Subject: test message
Content-Transfer-Encoding: base64
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"

PGh0bWw+CjxoZWFkPgo8L2hlYWQ+Cjxib2R5PgpUaGlzIGlzIGEgdGVzdAo8L2JvZHk+CjwvaHRt
bD4K

>>> old_content = msg.get_payload(decode=True)
>>> old_content
'<html>\n<head>\n</head>\n<body>\nThis is a test\n</body>\n</html>\n'
>>> new_content = old_content
>>> charset = msg.get_content_charset()
>>> del msg['content-transfer-encoding']
>>> msg.set_payload(new_content, charset)
>>> msg
>From nobody Tue May 13 17:32:32 2014
From: me
To: you
Subject: test message
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64

PGh0bWw+CjxoZWFkPgo8L2hlYWQ+Cjxib2R5PgpUaGlzIGlzIGEgdGVzdAo8L2JvZHk+CjwvaHRt
bD4K

>>>



-- 
Mark Sapiro <mark at msapiro.net>        The highway is for gamblers,
San Francisco Bay Area, California    better use your sense - B. Dylan


More information about the Mailman-Developers mailing list