将CSV的内容作为电子邮件中的表格发送?

时间:2014-11-27 10:48:34

标签: python email html-email

如何使用Python将CSV的内容作为电子邮件中的表格发送?

示例文件:

name,age,dob

xxx,23,16-12-1990

yyy,15,02-11-1997

2 个答案:

答案 0 :(得分:0)

检查this链接。

将csv作为第三个参数传递给server.sendmail()方法

答案 1 :(得分:0)

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