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:
#!/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:
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
. Also, my html.format()
statement is already occupied by image processing, so i cannot use .format(df.to_html())
there, can I?
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.