将CSV内容作为表格发送到电子邮件中? [英] Send the contents of a CSV as a table in an email?

查看:112
本文介绍了将CSV内容作为表格发送到电子邮件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python在电子邮件中以表格形式发送CSV内容?

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

示例文件:

name,age,dob

xxx,23,16-12-1990

yyy,15,02-11-1997

推荐答案

您可以使用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()

更好的方法:

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

这篇关于将CSV内容作为表格发送到电子邮件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆