问题
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