From b601a58008eca8cf569324ea6258cf5e62ac0a3f Mon Sep 17 00:00:00 2001 From: Kang Date: Wed, 27 Feb 2019 12:27:55 +0800 Subject: [PATCH] MIME multipart/alternative: Plain text first. (RFC2046) --- feed2exec/email.py | 15 ++++++++++++--- feed2exec/tests/test_plugins.py | 12 ++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/feed2exec/email.py b/feed2exec/email.py index 848327a..0cb9607 100644 --- a/feed2exec/email.py +++ b/feed2exec/email.py @@ -48,6 +48,9 @@ def make_message(feed, item, to_addr=None, cls=email.message.Message): cs.body_encoding = '8bit' msg = MIMEMultipart('alternative', boundary) html_parts = [] + msg_attach_queue_plain = [] + msg_attach_queue_html = [] + msg_attach_queue_part = [] for content in params.get('content', []): if not content.value: continue @@ -62,7 +65,7 @@ def make_message(feed, item, to_addr=None, cls=email.message.Message): html.replace_header('Content-Transfer-Encoding', '8bit') if subtype == 'html': html_parts.append(content.value) - msg.attach(html) + msg_attach_queue_html.append(html) if not msg.get_payload() and params.get('summary'): # no content found, fallback on summary @@ -74,9 +77,11 @@ def make_message(feed, item, to_addr=None, cls=email.message.Message): _subtype=subtype, _charset=cs) if subtype == 'plain': msg = part + # clear HTML attach queue + msg_attach_queue_html = [] else: html_parts.append(params.get('summary')) - msg.attach(part) + msg_attach_queue_part.append(part) for content in html_parts: # plain text version available params['content_plain'] = html2text_filter.parse(content) @@ -86,7 +91,11 @@ def make_message(feed, item, to_addr=None, cls=email.message.Message): text = MIMEText(body.encode('utf-8'), _subtype='plain', _charset=cs) text.replace_header('Content-Transfer-Encoding', '8bit') - msg.attach(text) + msg_attach_queue_plain.append(text) + + for _m in msg_attach_queue_plain + msg_attach_queue_part + msg_attach_queue_html: # final output + msg.attach(_m) # According to RFC 2046, plain text comes first, better. + payload = msg.get_payload() if len(payload) == 1: msg = payload.pop() diff --git a/feed2exec/tests/test_plugins.py b/feed2exec/tests/test_plugins.py index 8bac116..cdd25b0 100644 --- a/feed2exec/tests/test_plugins.py +++ b/feed2exec/tests/test_plugins.py @@ -78,12 +78,6 @@ Precedence: list Auto-Submitted: auto-generated Archived-At: http://www.example.com/blog/post/1 ---===============testboundary== -Content-Type: text/html; charset="utf-8" -MIME-Version: 1.0 -Content-Transfer-Encoding: 8bit - -This is the body, which should show instead of the above --===============testboundary== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 @@ -94,6 +88,12 @@ http://www.example.com/blog/post/1 This is the body, which should show instead of the above +--===============testboundary== +Content-Type: text/html; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: 8bit + +This is the body, which should show instead of the above --===============testboundary==-- ''' # noqa assert (expected % feed2exec.__version__) == message.read() -- GitLab