开发者

How do I generate a multipart/mime message with correct CRLF in Python?

开发者 https://www.devze.com 2023-01-04 01:13 出处:网络
I need to generate a multipart/mime messag开发者_如何学运维e to send as a response to a HTTP request but am hitting either a bug or limitation in the Python email.* package.

I need to generate a multipart/mime messag开发者_如何学运维e to send as a response to a HTTP request but am hitting either a bug or limitation in the Python email.* package.

The problem is that using Python 2.6, the message.as_string() call below generates a string with \n rather that CRLF as the line endings:

message = MIMEMultipart()
for image in images:
    f = image.open('rb')
    img = MIMEImage(f.read(), _encoder=encode_7or8bit)
    message.attach(img)


message.as_string()

There doesn't seem to be any way to persuade it to use the (MIME standard) CRLF. The Generator class that seems it should be able to do this, doesn't.

What have other people done to get round this?


This was a bug in Python that has now been fixed: http://hg.python.org/lookup/r85811

It should now be possible to use the MIME libraries over non-email transports and have sensible things happen.


What about a simple hack

message.as_string().replace('\n', '\r\n')

? Inelegant, but should work (and a bug report should be entered at the Python tracker).

0

精彩评论

暂无评论...
验证码 换一张
取 消