【问题标题】:Send the contents of a CSV as a table in an email?将 CSV 的内容作为表格发送到电子邮件中?
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      检查this链接。

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

      【讨论】:

        猜你喜欢
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 2019-12-06
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多