Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

How do I send a html content in mail body using python?

$
0
0

I am sending a mail by reading an excel file using pandas as dataframe. Then I am converting that dataframe using df.to_html with CSS style wrapped. Below is my code.

from email.mime.text import MIMEText
import email.message
import numpy as np
import pandas as pd
import email
import smtplib
import xlrd
filename = 'exl_sheet.xls'
df = pd.read_excel(filename)
html_string = '''<html>
  <head><title>HTML Pandas Dataframe with CSS</title></head>
  <link rel="stylesheet" type="text/css" href="df_style.css"/>
  <body>
  {table}
  </body>
</html>.
             '''
df_html = html_string.format(table=df.to_html(classes='mystyle'))
sender = "sender@gmail.com"
receiver = "receiver@gmail.com"
password = 'xxxxxxxxx'
server = 'smtp.gmail.com:587'
msg = email.message.EmailMessage()
msg['Subject'] = 'Train Data'
msg['From'] = sender
msg['To'] = receiver
msg = MIMEText(df_html, 'html')
print(msg)
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(sender, password)
server.sendmail(sender, receiver, msg)
server.quit()

The output of print(msg) is :

enter image description here

However, the message I am receiving is as below :

enter image description here

That is I am not receiving the table with style. Please suggest.


Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>