Skip to content

Instantly share code, notes, and snippets.

@boyska
Last active December 24, 2020 11:45
Show Gist options
  • Save boyska/76744cd3c1b6143ebfe5db152e548871 to your computer and use it in GitHub Desktop.
Save boyska/76744cd3c1b6143ebfe5db152e548871 to your computer and use it in GitHub Desktop.
This post_process plugin for rss2email/rss2email addresses #101 in a pluggable way.
'''
Converts html messages in multipart messages with both text/html and text/plain parts
post-process = rss2email.post_process.multipart process
'''
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import html2text
def add_plain(guid: str, message, html: str):
headers = message.items()
msg = MIMEMultipart('related')
for name, value in headers:
if name.lower().startswith('content-'):
continue
msg[str(name)] = value
alternative = MIMEMultipart('alternative')
msg.attach(alternative)
html_part = MIMEText(html, _subtype='html')
alternative.attach(html_part)
text_content = html2text.html2text(html=html, baseurl=guid)
text_part = MIMEText(text_content)
alternative.attach(text_part)
return msg
def process(feed, parsed, entry, guid, message):
if message.get_content_type() == 'text/html':
m = add_plain(guid, message, message.get_payload())
return m
if message.is_multipart():
# we could support multipart messages, but let's postpone it
return message
return message
@boyska
Copy link
Author

boyska commented Dec 24, 2020

This requires that this branch is merged upstream: https://github.com/boyska/rss2email/tree/improve-post-process

Otherwise, post-process plugins cannot make so "radical" modifications of the Message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment