Sending HTML table in body of email in Airflow

Viewed 925

We have an html table trying to send in body of email by using below code and code is keep on running but havent received any email

from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib
import sys


recipients = ['ravi1234@gmail.com'] 
emaillist = [elem.strip().split(',') for elem in recipients]
msg = MIMEMultipart()
msg['Subject'] = "Insurance details"
msg['From'] = 'from@domain.com'


html = """\
<html>
  <head></head>
  <body>
    {0}
  </body>
</html>
""".format(df_test.to_html())

part1 = MIMEText(html, 'html')
msg.attach(part1)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.sendmail(msg['From'], emaillist , msg.as_string())

is there any way we can send it through email body having html table

1 Answers

Try adding this before you send the message:

part1 = MIMEText(html,_subtype='html')

and remove the msg.attach(part1)

Related