我正在尝试在电子邮件正文中发送带有一些文本的 Excel 表格
这是我尝试过的:
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()
目前正在按要求发送表格,但是“嗨, 相关数据请查附件 “ 文本未添加到电子邮件中。
任何帮助都会很棒!
我认为这是因为您在同一封电子邮件中包含纯文本和 HTML 部分。 尝试这样的事情:
# 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>
"""