Send the contents of a CSV as a table in an email?

别来无恙 提交于 2020-02-29 09:58:06

问题


How can I send the contents of a CSV as a table in an email using Python?

Example file:

name,age,dob

xxx,23,16-12-1990

yyy,15,02-11-1997

回答1:


you can use smtplib:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# just an example, you format it usng html
html = "<html><body><table><tr><td>name</td><td><age></td><td>dob</td<tr></table></body></html>"
msg1 = MIMEText(html,'html')
msg = MIMEMultipart("test")
msg['Subject'] = "Name of subject"
msg['From'] = "your@email.com"
msg['To'] = "reciver@gmail.com"
msg.attach(msg1)

server = smtplib.SMTP("smpt_server",port)         # example smtplib.smtp("smtp.gmail.com,587)
server.starttls()

server.login("your@gmail.com","login_password")
server.sendmail("your@email.com","reciver@gmail.com",msg.as_string())
server.quit()

better you can do:

msg1 = MIMEText(f.open("file.csv").read())
msg.attach(msg1)



回答2:


Check this link.

Just pass your csv as third argument to server.sendmail() method



来源:https://stackoverflow.com/questions/27168764/send-the-contents-of-a-csv-as-a-table-in-an-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!