Я не могу понять, почему мой код Python не может прикрепить таблицу HTML (часть2) и простое предложение (часть1) к одному и тому же письму.Я попытался прокомментировать одну строку message.attach(part2)
, и она работает наоборот.Что-то не так с объектом message
.
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "xyz@gmail.com"
receiver_email = ["abc@qwer.com","def@gmail.com"]
password = getpass.getpass()
message = MIMEMultipart("alternative")
message["From"] = sender_email
message["To"] = ", ".join(receiver_email)
message["Subject"] = "Report"
# Create the plain-text and HTML version of your message
pd.read_csv('tmp.csv').to_html('report_html.html')
html=open('report_html.html').read()
part1=MIMEText("Following are the instances currently running:\n","plain")
part2=MIMEText(html,"html")
message.attach(part1)
message.attach(part2)
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email,password)
server.sendmail(sender_email, receiver_email,
message.as_string())
Моя отдельная попытка с выводом части 1 и части 2 приведена ниже: