[英]add image and data frame into a single html body of email
trying to embed both a dataframe and image together in the body of the email.尝试在电子邮件正文中同时嵌入数据框和图像。 I was able to adapt the folowing code from email.examples to embed the image successfully:
我能够调整email.examples 中的以下代码以成功嵌入图像:
#!/usr/bin/env python3
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
# Create the base text message.
msg = EmailMessage()
msg['Subject'] = "Ayons asperges pour le déjeuner"
msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
Address("Fabrette Pussycat", "fabrette", "example.com"))
msg.set_content("""\
Salut!
Cela ressemble à un excellent recipie[1] déjeuner.
[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
--Pepé
""")
# Add the html version. This converts the message into a multipart/alternative
# container, with the original text message as the first part and the new html
# message as the second part.
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')
# note that we needed to peel the <> off the msgid for use in the html.
# Now add the related image to the html part.
with open("roasted-asparagus.jpg", 'rb') as img:
msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
cid=asparagus_cid)
# Make a local copy of what we are going to send.
with open('outgoing.msg', 'wb') as f:
f.write(bytes(msg))
# Send the message via local SMTP server.
with smtplib.SMTP('localhost') as s:
s.send_message(msg)
However, not sure how to add a dataframe in addition.但是,不确定如何另外添加数据框。 I tried a couple of functions below which i believe replacing the old
MIMEMultipart
legacy class:我尝试了以下几个函数,我相信它们会替换旧的
MIMEMultipart
遗留类:
df = pd.DataFrame(
{'A': [419,382, 382, 382, 411, 411, 411],
'B':[2,2,3,3,3,3,6],
'C': np.random.randn(7)*10,
'D': list('abcdefg')
}
)
msg.add_alternative(df.to_html())
#msg.get_payload()[1].add_alternative(df.to_html())
#msg.add_attachment(df.to_html())
#msg.get_payload()[1].add_attachment(df.to_html())
but getting contentmanager
KeyError
.但得到
contentmanager
KeyError
。 Also, my html.format()
statement is already occupied by image processing, so i cannot use .format(df.to_html())
there, can I?另外,我的
html.format()
语句已经被图像处理占用了,所以我不能在那里使用.format(df.to_html())
,可以吗?
Try adding your dataframe using variable substitutions, just like you did for your image :尝试使用变量替换添加数据框,就像您为图像所做的一样:
df = pd.DataFrame(
{'A': [419,382, 382, 382, 411, 411, 411],
'B':[2,2,3,3,3,3,6],
'C': np.random.randn(7)*10,
'D': list('abcdefg')
}
)
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}" />
{dataframe_table}
</body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1], dataframe_table=df.to_html()), subtype='html')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.