使用不带附件的python发送csv表

时间:2019-02-26 09:29:06

标签: python csv sendmail

我正在尝试使用python发送一个csv表,但是它不起作用。我不需要附件,我只需要此csv中的tabla(数据)。

这是我的代码:

me = 'mail1'
    password = 'password'
    server = 'smtp.gmail.com:587'
    you = 'mail2'
    text = """
    Hello, Friend.

    Here is your data:

    {table}

    Regards,

    Me"""

    html = """
    <html><body><p>Hello, Friend.</p>
        <p>Here is your data:</p>
    {table}
        <p>Regards,</p>
    <p>Me</p>
    </body></html>
    """

    with open('test.csv') as input_file:
            content=input_file.read().decode("UTF-8")
            reader = csv.reader(content)
        data = list(reader)

    text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid"))
    html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))

    message = MIMEMultipart(
        "alternative", None, [MIMEText(text), MIMEText(html,'html')])

    message['Subject'] = "Your data"
    message['From'] = me
    message['To'] = you
    server = smtplib.SMTP(server)
    server.ehlo()
    server.starttls()
    server.login(me, password)
    server.sendmail(me, you, message.as_string())
    server.quit()

我怎么了?我正在使用Python 2.7

编辑:

Error: "'ascii' codec can't decode byte 0xc3 in position 0: 
     ordinal not in range(128) first failed due to some error "

0 个答案:

没有答案