我正在寻找解决这个问题的方法:通过SMTP发送包含HTML的电子邮件。我正在跟踪文档here https://docs.python.org/3/library/email.html和here https://docs.python.org/3/library/email.examples.html,但找不到一个解决方法来集成HTML以添加图像部分:
asparagus_cid = make_msgid()
msg.add_alternative("""\
<html>
<head></head>
<body>
<p>Salut!</p>
<p>Cela ressemble à un excellent
<a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718">
recipie
</a> déjeuner.
</p>
<img src="cid:{asparagus_cid}" />
</body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
with open("roasted-asparagus.jpg", 'rb') as img:
msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
cid=asparagus_cid)
编辑:
要插入
python object
,可以使用f-string
s。它们不笨重,并且平滑地自附着到任何字符串对象================================================================
旧答案:
可以使用
EmailMessage
类的实例发送带有html的电子邮件。subtype参数允许您将消息的格式从纯文本更改为html等格式现在,请看图片。您可以将其添加到html正文中,就像在静态网页中一样
相关问题 更多 >
编程相关推荐