首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用smtplib在电子邮件中将熊猫数据作为CSV

如何使用smtplib在电子邮件中将熊猫数据作为CSV
EN

Stack Overflow用户
提问于 2022-09-27 16:23:10
回答 1查看 49关注 0票数 0

我正在和熊猫一起工作,但是我希望Dataframe使用df.to_csv创建csv文件并将其附加到电子邮件中,这样收件人就可以将它作为附件使用,而htmldf.to_html也能很好地工作。

如何将df.to_csv附加到电子邮件中,以便在其中输入html视图。

下面是我用来发送电子邮件的代码部分示例。

代码语言:javascript
复制
import smtplib
import pandas as pd
from tabulate import tabulate
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText

# mail vars
SMTP_SERVER = 'smtp_server@example.com'
FROM = 'some_address@example.com'
TO = ['address@example.com']
SUBJECT = 'test attachment e-mail'
EMAIL_TEMPLATE = """\
<html>
  <head>
  <style>
  table, th, td {{font-size:9pt; border:1px solid black; border-collapse:collapse; text-align:left; background-color:LightGray;}}
  th, td {{padding: 5px;}}
  </style>
  </head>
  <body>
     Dear Team,<br><br>
     Please Find the Project-wise Report with their respectove Owners ID and e-mail address.!<br><br>
     {} <br><br>
    Kind regards.<br>
    STR TEAM.
  </body>
</html>"""

DataFrame:

代码语言:javascript
复制
df = pd.DataFrame("/tmp/fix.txt")

函数到sen邮件:

代码语言:javascript
复制
def send_email():
    server = smtplib.SMTP(SMTP_SERVER)
    msg = EmailMessage()
    msg['Subject'], msg['From'], msg['To'] = SUBJECT, FROM, TO
    msg.set_content("Text version of your html template")
    msg.add_alternative(
        EMAIL_TEMPLATE.format(df.to_html(index=False)),
        subtype='html'
    )
    server.send_message(msg)


if __name__ == '__main__':
    send_email()

审判:

我试过用下面的东西但没用..。

msg.add_alternative(EMAIL_TEMPLATE.format(df.to_csv(index=False)),subtype='csv')

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-27 18:26:58

您需要使用,以便可以将数据作为.csv附加。

试试这个:

代码语言:javascript
复制
import smtplib
import pandas as pd
from tabulate import tabulate
from email.message import EmailMessage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
import io

# mail vars
SMTP_SERVER = 'smtp_server@example.com'
FROM = 'some_address@example.com'
TO = ['address@example.com']
SUBJECT = 'test attachment e-mail'
EMAIL_TEMPLATE = """\
<html>
  <head>
  <style>
  table, th, td {{font-size:9pt; border:1px solid black; border-collapse:collapse; text-align:left; background-color:LightGray;}}
  th, td {{padding: 5px;}}
  </style>
  </head>
  <body>
     Dear Team,<br><br>
     Please Find the Project-wise Report with their respectove Owners ID and e-mail address.!<br><br>
     {} <br><br>
    Kind regards.<br>
    STR TEAM.
  </body>
</html>"""

def df_to_csv(df):
    with io.StringIO() as buffer:
        df.to_csv(buffer)
        return buffer.getvalue()

def send_email():    
    multipart = MIMEMultipart()
    multipart['Subject'],  multipart['From'], multipart['To'] = SUBJECT, FROM, TO
    attachment = MIMEApplication(df_to_csv(df))
    attachment['Content-Disposition'] = 'attachment; filename="dataframe.csv"'
    multipart.attach(attachment)
    multipart.attach(MIMEText(EMAIL_TEMPLATE, 'html'))
    server = smtplib.SMTP(SMTP_SERVER)
    server.sendmail(FROM, TO, multipart.as_string())
    server.quit()

if __name__ == '__main__':
    send_email()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73870825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档