当前位置: 代码迷 >> python >> 如何在python电子邮件正文中添加多个数据框
  详细解决方案

如何在python电子邮件正文中添加多个数据框

热度:67   发布时间:2023-06-13 16:47:15.0

我想在电子邮件正文中插入两个数据框,但我可以得到一个,但不能两个都得到。

这是我的代码

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")

输出是

我的目标是将尽可能多的数据帧添加到电子邮件正文中并发送电子邮件

快速浏览一下,我认为您的问题较少是由代码引起的,而更多是因为使用了两组htmlhead标签。

某些浏览器/客户端可能无法很好地解释这一点,仅接受第一次出现。 HTML文档仅应具有一套。 身体标签也是如此。

调整字符串以反映这一点,然后查看是否可以解决您的问题。