【发布时间】:2016-02-11 04:38:04
【问题描述】:
我正在尝试将在 Pandas Python 中创建的两个数据帧作为 html 格式发送到从 python 脚本发送的电子邮件中。
我想写一个文本和表格,并为另外两个数据框重复此操作,但脚本无法附加多个 html 块。 代码如下:
import numpy as np
import pandas as pd
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender = "blabla@gmail.com"
recipients = ['albalb@gmail.com']
msg = MIMEMultipart('alternative')
msg['Subject'] = "This a reminder call " + time.strftime("%c")
msg['From'] = sender
msg['To'] = ", ".join(recipients)
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = df[['SYMBOL','ARBITRAGE BASIS %']].to_html()
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
username = 'blabla@gmail.com'
password = 'blahblah'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(sender, recipients, msg.as_string())
server.quit()
print("Success")
我收到一封电子邮件,其中只有最后一部分作为电子邮件正文中的格式化 html 表格。第 1 部分文本未出现。怎么了?
【问题讨论】: