简体   繁体   English

使用email.mime时无法在电子邮件正文中看到熊猫数据框

[英]Unable to see the pandas data frame in the email body while using email.mime

I have the following dataframe(my_df) in pandas: 我在熊猫中有以下数据框(my_df):

Date    Value1  Value2  Value3
01/05/2018  407 8   304
02/05/2018  879 3   82
03/05/2018  998 407 435
04/05/2018  399 479 349
05/05/2018  394 299 705
06/05/2018  840 524 710

I convert the dataframe into html format using the following code: 我使用以下代码将数据框转换为html格式:

my_df.to_html('df1.html')

When trying to use it in my email as the body, it sends email with all the text, but not the dataframe. 当尝试在我的电子邮件中将其作为正文使用时,它将发送包含所有文本的电子邮件,但不发送数据框。 Any idea what I might be doing wrong? 知道我做错了什么吗?

I am using the following code to send an email: 我正在使用以下代码发送电子邮件:

strFrom = 'xyz'
    strTo = ['xyz']
    cc = ['abc']

    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = 'test'
    msgRoot['From'] = strFrom
    msgRoot['To'] = ",".join(strTo)
    msgRoot['Cc'] = ",".join(cc)
    msgRoot.preamble = 'This is a multi-part message in MIME format.'
    text1 = "\nABC:\n\nTable 1(xyz)"    

    part1 = MIMEText(text1, 'plain')
    part2 = MIMEText('df1.html', 'html')
    msgRoot.attach(part1)
    msgRoot.attach(part2)    

    server = smtplib.SMTP(email_host)
    server.set_debuglevel(1)
    server.sendmail(strFrom, strTo, msgRoot.as_string())
    server.quit()

Please help. 请帮忙。

You can directly render your dataframe html to the email body. 您可以将数据框html直接呈现到电子邮件正文。

Ex: 例如:

strFrom = 'xyz'
strTo = ['xyz']
cc = ['abc']

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test'
msgRoot['From'] = strFrom
msgRoot['To'] = ",".join(strTo)
msgRoot['Cc'] = ",".join(cc)
msgRoot.preamble = 'This is a multi-part message in MIME format.'

text1 =  """<html>
  <head></head>
  <body>
    <h4>ABC: Table 1(xyz)</h4>
    {0}
  </body>
</html>
""".format(df_test.to_html())


part1 = MIMEText(text1, 'html')
msgRoot.attach(part1)


server = smtplib.SMTP(email_host)
server.set_debuglevel(1)
server.sendmail(strFrom, strTo, msgRoot.as_string())
server.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM