Python发送多部分与身体内容 [英] Python Emailing Multipart with body content
问题描述
我不能发送电子邮件在python与身体作为多部分电子邮件。我尝试的所有内容已将所有内容导出为附件,我无法将文字或HTML显示在正文中。
I can't send an e-mail in python with a body as a multipart email. Everything I've tried has resulted in all of the content as attachments, and I can't get the text or html to show up in the body.
msg = MIMEMultipart()
if msg_mime_type == 'text' or not msg_mime_type:
new_body = MIMEText(body, 'text')
elif msg_mime_type == 'image':
new_body = MIMEImage(body)
elif msg_mime_type == 'html':
new_body = MIMEText(body, 'html')
new_body.add_header('Content-Disposition', 'inline', filename='body')
msg.set_payload(new_body) #also tried msg.attach(new_body)
我需要使用 Multipart
,以便我也可以添加附件,但是为了简单起见,我保留了该代码。
I need to use a Multipart
so that i can also add attachments, but I kept that code out for simplicity.
推荐答案
您需要指定这些部分是彼此的替代品,例如 multipart / alternative
mime类型:
You need to specify that the parts are alternatives of one another, e.g. the multipart/alternative
mime type:
msg = MIMEMultipart('alternative')
默认值为 mixed
;请参阅电子邮件库示例。
请注意,要创建具有附件和替代(HTML / CSS)选项的电子邮件,您需要具有顶级 multipart / related
容器包含替代
部分作为第一个条目。
Note that to create an email with both attachments and an alternative (HTML / CSS) option you'll need to have a top-level multipart/related
container that contains the alternative
parts as the first entry.
这篇关于Python发送多部分与身体内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!