It seems like there should be a better way to do this. I have a plain text email, and I'd like to convert it to a multipart email. I can do it like this:
import commonmark
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.parser import BytesParser
raw_msg = b'''
To: you@example.com
From: me@example.com
Subject: This is a sample
# This should be h1
Something
## This should be h2
Email from markdown? Sweet
'''.strip()
msg = BytesParser().parsebytes(raw_msg)
html = commonmark.commonmark(msg.get_payload())
new_msg = MIMEMultipart()
for key in msg.keys():
new_msg[key] = msg.get(key)
new_msg.attach(MIMEText(msg.get_payload()))
new_msg.attach(MIMEText(html, 'html'))
print(str(new_msg))
Which appears to work, but it also seems clunky to me. Is there a way in Python to just create a multipart email from my original message? Or am I doing it the right way?