From: W. Trevor King Date: Thu, 24 Jan 2013 04:41:50 +0000 (-0500) Subject: email: Fix _flatten() implementation for non-ASCII bodies X-Git-Tag: v3.0~4 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8a907f9;p=rss2email.git email: Fix _flatten() implementation for non-ASCII bodies The email header should be flattened to ASCII with funky encoded headers [1], but the body may be encoded in a non-ASCII-compatible charset (e.g. UTF-16-LE). The old _flatten() implementation used the body charset to encode the entire message, which could garble the header. This patch uses BytesGenerator, which takes advantage of email.charset.Charset's separate fields for the header encoding and body encoding. [1]: http://docs.python.org/3/library/email.header.html Signed-off-by: W. Trevor King --- diff --git a/rss2email/email.py b/rss2email/email.py index 4fac799..8bf3eb6 100644 --- a/rss2email/email.py +++ b/rss2email/email.py @@ -21,10 +21,12 @@ from email.charset import Charset as _Charset import email.encoders as _email_encoders +from email.generator import BytesGenerator as _BytesGenerator from email.header import Header as _Header from email.mime.text import MIMEText as _MIMEText from email.utils import formataddr as _formataddr from email.utils import parseaddr as _parseaddr +import io as _io import smtplib as _smtplib import subprocess as _subprocess @@ -226,7 +228,10 @@ def _flatten(message): b'' b"\x00Y\x00o\x00u\x00'\x00r\x00e\x00 \x00g\x00r\x00e\x00a\x00t\x00,\x00 \x00\x96\x03\xb5\x03\xcd\x03\xc2\x03!\x00\\\x00n\x00" """ - return message.as_string().encode(str(message.get_charset())) + bytesio = _io.BytesIO() + generator = _BytesGenerator(bytesio) # use policies for Python >=3.3 + generator.flatten(message) + return bytesio.getvalue() def sendmail_send(sender, recipient, message, config=None, section='DEFAULT'): if config is None: