I am trying to send an excel table in the body of an email that is accompanied by some text
This is what I've tried:
import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['Subject'] = "Lists"
msg['From'] = sender_email
msg['To'] = receiver_email
html = html_file
text = "Hi,\n Please find attached the relevant data\n"
msg.attach(MIMEText(text, 'plain'))
msg.attach(MIMEText(html, 'html'))
s = smtplib.SMTP('smtp.gmail.com',587)
s.connect('smtp.gmail.com',587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender_email, password)
print("login success")
s.sendmail(sender_email, receiver_email, msg.as_string())
print("email has been sent")
s.quit()
Currently this is sending out the table as required, however the "Hi,\n Please find attached the relevant data\n" text is not being added to the email.
Any help would be great!
I think it's because you have plain text and HTML parts in the same email. Try something like:
# Assuming 'html_file' contains your HTML table as a string
html = """
<html>
<body>
<p>Hi,<br>
Please find attached the relevant data.<br><br>
</p>
""" + html_file + """
</body>
</html>
"""