【发布时间】:2013-08-05 14:49:22
【问题描述】:
我正在使用 Python 2.7 并尝试使用 smtplib/MIMEMultipart 发送电子邮件。我想发送一封包含多个部分的电子邮件,例如,一条短信和一条 html 消息。我不希望他们成为替代品。我想让它显示文本消息(内联),然后是 html(内联)
将来,我还想添加图片。因此,消息将包含所有内联的文本、html 和图像。
这是我目前拥有的,它会生成一条文本消息,然后生成 html 作为附件
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
s = smtplib.SMTP('localhost')
#this part works
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = from
msg['To'] = to
html_str = """<html><head></head><body><p>Test</p></body></html>"""
#this shows up with "This is my text" inline and the html as an attachment
text = MIMEText("This is my text", 'plain')
html = MIMEText(html_str, 'html')
msg.attach(text)
msg.attach(html)
s.sendmail(fromEmail, toEmail, msg.as_string())
s.quit()
如何在电子邮件中添加多个内联部分? 谢谢你的帮助
【问题讨论】:
-
欢迎来到 Stack Overflow。 SO 是一个问答网站,但您的帖子中没有包含问题。您的具体问题是什么?
-
另外,您可能对Content-Disposition: 标头感兴趣。
-
谢谢罗伯。我已经更新了我的问题。我不确定如何将多个部分添加到电子邮件中,使其全部显示为内联(而不是作为附件)。
-
查看这个答案:stackoverflow.com/questions/3362600/… 它提供了如何附加附加附件的示例。如果您想进入简单模式,请查看ginstrom.com/code/mailer.html,其中添加附件只是传递文件名数组作为对象创建的一部分。漂亮。
-
Synthesizerpatel,谢谢你的回复,但这个答案是关于多个附件的。我正在寻找多个 inline 片段(它们不应该是附件)
标签: python email mime-types