I'm fairly new to Python, trying to help automate a few things at work. I'm trying to use it to send out a message to a list of customers who have purchased with us in the past three months in an excel spreadsheet. I've got the below but it currently sends the html message as plain text. I've managed to get it to send the html message properly before, but wasn't able to combine pulling data from excel using pandas with sending the html message.
Any help would be very much appreciated.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
your_name = "NAME"
your_email = "EMAIL"
your_password = "PASSWORD"
server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls()
server.ehlo()
server.login(your_email, your_password)
email_list = pd.read_excel("python_test.xlsx")
all_names = email_list['Name']
all_emails = email_list['Email']
for idx in range(len(all_emails)):
name = all_names[idx]
email = all_emails[idx]
subject = ‘Subject’
message = MIMEMultipart('mixed')
BODY = html = """\
<html>
Html message goes here
</html>
"""
HTML_BODY = MIMEText(BODY, 'html')
message.attach(HTML_BODY)
full_email = ("From: {0} <{1}>\n"
"To: {2} <{3}>\n"
"Subject: {4}\n\n"
"{5}"
.format(your_name, your_email, name, email, subject, message,))
try:
server.sendmail(your_email, [email], full_email)
print('Email to {} successfully sent!\n\n'.format(email))
except Exception as e:
print('Email to {} could not be sent :( because {}\n\n'.format(email, str(e)))
server.close()