email: Use Charsets to set the Content-Transfer-Encoding
authorW. Trevor King <wking@tremily.us>
Thu, 24 Jan 2013 04:39:44 +0000 (23:39 -0500)
committerW. Trevor King <wking@tremily.us>
Thu, 24 Jan 2013 04:49:42 +0000 (23:49 -0500)
This ensures that payload encoding/decoding happens appropriately, and
allows 7-bit-clean data to be sent with a 7bit CTE, even when the
use-8bit setting is on.

Signed-off-by: W. Trevor King <wking@tremily.us>
rss2email/email.py

index 3b1e2176b37ee2a4903431bb272852ce55c1a687..90d245da150e46a3bf6de39f51c8e66a6eb61bb8 100644 (file)
@@ -19,6 +19,8 @@
 """Email message generation and dispatching
 """
 
+from email.charset import Charset as _Charset
+import email.encoders as _email_encoders
 from email.header import Header as _Header
 from email.mime.text import MIMEText as _MIMEText
 from email.utils import formataddr as _formataddr
@@ -116,8 +118,9 @@ def get_message(sender, recipient, subject, body, content_type,
     message['Subject'] = _Header(subject, subject_encoding)
     if config.getboolean(section, 'use-8bit'):
         del message['Content-Transfer-Encoding']
-        message['Content-Transfer-Encoding'] = '8bit'
-        message.set_payload(body)
+        charset = _Charset(body_encoding)
+        charset.body_encoding = _email_encoders.encode_7or8bit
+        message.set_payload(body, charset=charset)
     if extra_headers:
         for key,value in extra_headers.items():
             encoding = guess_encoding(value, encodings)