[英]How to attched pandas dataframe as CSV over the e-mail using smtplib
I am working with a pandas Dataframe which works fine however i want that DataFrame to create csv
file using df.to_csv
and attach that to an e-mail So, that recipient can have that as an attachment as well as html
, df.to_html
work好的。
我如何在我希望html
視圖完好無損的電子郵件中附加df.to_csv
。
下面是我用於發送電子郵件的代碼部分示例。
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:
df = pd.DataFrame("/tmp/fix.txt")
Function 發送郵件:
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')
您需要使用io.StringIO
所以您可以將 dataframe 附加為.csv
。
嘗試這個:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.