【问题标题】:Send Table contents from CSV file in Outlook Mail Body using Python使用 Python 从 Outlook 邮件正文中的 CSV 文件发送表格内容
【发布时间】:2021-09-02 04:25:28
【问题描述】:

我编写了一个代码来发送 Outlook 邮件,该邮件的正文应包含 CSV 文件的内容,邮件部分工作正常。但表格似乎变形了。

[![邮件正文][1]][1]

所以无论如何这里都有安排。让它变得漂亮。

这是我的代码:

def sendMailt():

    print("*** SENDING MAIL ****")
    email_user = 'ABC@domain.com'
    email_send = 'DCF@domain.com'

    subject = ''

    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To'] = email_send
    msg['Subject'] = subject
    body = 'Hi Team , Please Open This attachment for Folderstat Report,'
    msg.attach(MIMEText(body, 'plain'))

    text = """
    Hello, Friend.

    Here is your data:

    {table}

    Regards,

    Me"""

    with open(filtered_CSV) as input_file:
        reader = csv.reader(input_file)
        data = list(reader)

    text = text.format(table=tabulate(data, headers=['Unnamed: 0','id','path','name','extension','size','FolderSize in GB','LastAccessTime','LastModifiedTime','LastCreationTime','folder','Total Number of files','Absolute File Count','depth','parent','uid','Oldest File Timestamp','Recent File Timestamp','Folder Modified Timestamp','Status','md5]'] ,tablefmt='orgtbl'))
    server = smtplib.SMTP('domain')

    sender = 'ABC@domain.com'
    reciever = ['DCF@domain.com']
    server.sendmail(sender, reciever, text)
    server.quit()
    print("Mail SEND")

当我收到邮件时,也看不到主题或收件人 ID [1]:https://i.stack.imgur.com/y5Gxy.png

【问题讨论】:

    标签: python outlook smtp


    【解决方案1】:

    如果你在 tablefmt 中使用 html,你可以得到适当格式的数据

    from tabulate import tabulate
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    text = """
    Hello, Friend.
    
    Here is your data:
    
    {table}
    
    Regards,
    
    Me"""
    
    with open(filtered_CSV) as input_file:
        reader = csv.reader(input_file)
        data = list(reader)
    
    html = text.format(table=tabulate(data, headers="firstrow", tablefmt="html")
    # for html design ( you can add whatever style you want)
    html = html.replace('<table>', """<table border=1 style="
                            border-color:grey;
                            font-family: Helvetica, Arial;
                            font-size: 15px;
                            padding: 5px;">""")
    server = smtplib.SMTP('domain')
    msg = MIMEMultipart("alternative", None, [MIMEText(html, 'html')])
    msg['From'] = from address
    msg['To'] = to address
    msg['Subject'] = "your subject"
    
    server.sendmail(msg['From'], msg['To'], msg.as_string())
    server.quit()
    print("Mail SEND")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      相关资源
      最近更新 更多