1

Using Python, I made a table in Pandas and I converted it to html. I want to send the html into an embedded email now. I am capable of sending it to email as an attachment, but I want it in the body of the email. How do I go about doing this?

This saves the Pandas table (route_df) as an html file.

 with open('routesumm.html', 'w') as f:
    f.write("<h1>Route Summary for %s</h1>" % (branch,))
    f.write(route_df.to_html())

And this sends the html file as an attachment.

msg = MIMEMultipart()
msg['Subject'] = 'Route Summary for ' + branch
msg.attach(MIMEText('Route Summary'))
part = MIMEBase('application', "octet-stream")
f = 'routesumm.html'
part.set_payload(open(f, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % f)
msg.attach(part)

But I'd rather get the HTML file embedded into the email if possible. What's the best to go about doing this? Thanks for your help!

I've tried doing something like:

    for row in f:
        msg.attach(MIMEText(row, 'html'))

And it will put the first letter in the email, and then attach every other letter as an "htm" (not even html) file. Not sure what else to try? Any thoughts?

Emac
  • 1,098
  • 3
  • 18
  • 37
  • Mail systems have a max embed size. How big is your dataframe? – Itay Livni Jun 22 '17 at 17:47
  • Not enormous, some range from 10 rows to maybe 100 at the largest. That could be a limitation at some point but I'm sure that's not my problem yet. How would I get it to work with even the smallest of dataframes? – Emac Jun 22 '17 at 17:53
  • Related: [python-emailing-multipart-with-body-content](https://stackoverflow.com/questions/11995720/python-emailing-multipart-with-body-content) – stovfl Jun 22 '17 at 18:10
  • Yeah - that is small enough – Itay Livni Jun 22 '17 at 18:18

0 Answers0