我想在我的电子邮件正文中插入两个数据框,我可以获得一个但不能同时使用。
这是我的代码
import smtplib
import teradata
import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
table =[{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': 140}, {'account': 'Alpha Co', 'Jan': 200, 'Feb': 210, 'Mar': 215},{'account': 'Blue Inc', 'Jan': 50, 'Feb': 90, 'Mar': 95 }]
table1 =[{'account': 'fgsdgd LLC', 'Jan': 150, 'Feb': 200, 'Mar': 140}, {'account': 'fsdgsd Co', 'Jan': 200, 'Feb': 210, 'Mar': 215},{'account': 'Blue Inc', 'Jan': 50, 'Feb': 90, 'Mar': 95 }]
sql_canc_hour = pd.DataFrame(table)
sql1 = pd.DataFrame(table1)
me = "xxx@gmail.com"
you = "xxxxxx@gmail.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Account Details"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = " "
html = """\
<html>
<head></head>
<br>Hi Team,\n</br>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
{sql_canc_hour}
<script type="text/javascript">
$("table").addClass("table table-bordered table-striped table-responsive table-condensed");
</script>
</html>"""
html1 = """ <html>
<head></head>
<br>Hi Team,\n</br>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
{sql1}
<script type="text/javascript">
$("table").addClass("table table-bordered table-striped table-responsive table-condensed");
</script> """
html= html.format(sql_canc_hour = sql_canc_hour.to_html())
html1= html1.format(sql1 = sql1.to_html())
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
part3 = MIMEText(html1, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
msg.attach(part3)
# Send the message via local SMTP server.
s = smtplib.SMTP('XXXX@gmail.com')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
print ("Successfully sent email")
输出是 screen shot
我的目标是在电子邮件正文中添加尽可能多的数据框并发送电子邮件
答案 0 :(得分:0)
只是快速浏览一下,我认为您的问题不是由您的代码造成的,而是使用两组 html 和 head 标记的结果。
某些浏览器/客户端可能无法很好地解释这一点,只接受第一次出现。 HTML文档只应该有一组。对于 body 标签也是如此。
调整字符串以反映这一点,看看是否能解决您的问题。