【发布时间】:2014-11-27 10:48:33
【问题描述】:
如何使用 Python 将 CSV 的内容作为表格发送到电子邮件中?
示例文件:
name,age,dob
xxx,23,16-12-1990
yyy,15,02-11-1997
【问题讨论】:
标签: python email html-email
如何使用 Python 将 CSV 的内容作为表格发送到电子邮件中?
示例文件:
name,age,dob
xxx,23,16-12-1990
yyy,15,02-11-1997
【问题讨论】:
标签: python email html-email
你可以使用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)
【讨论】:
检查this链接。
只需将您的 csv 作为第三个参数传递给 server.sendmail() 方法
【讨论】: