【发布时间】:2019-05-06 04:20:17
【问题描述】:
似乎应该有更好的方法来做到这一点。我有一封纯文本电子邮件,我想将其转换为多部分电子邮件。我可以这样做:
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))
这似乎可行,但对我来说似乎也很笨重。在 Python 中有没有一种方法可以从我的原始消息中创建多部分电子邮件?还是我做对了?
【问题讨论】:
标签: python email email-attachments multipart