1

我的表格在控制台中打印得很好,但是当我通过电子邮件发送它时,它的对齐方式有点失真,有什么建议可以完美地发送它,就像它在控制台中打印一样?

from beautifultable import BeautifulTable
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
table = BeautifulTable()
a= ["name", "age", "class"]
b=["Jacob", 1, "boy"]

table.column_headers =a
table.append_row(b)
print table

def mailfunction(table):


    fromaddr = "sender@gmail.com"
    toaddr = "receiver@gmail.com"
    msg = MIMEMultipart()

    msg['From'] = fromaddr
    msg['To'] = toaddr

    msg['Subject'] = " TABLE PRINT"

    body = str(table)

    msg.attach(MIMEText(body, 'plain'))
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(fromaddr, "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()


mailfunction(table)

控制台输出:

+-------+-----+-------+
| name  | age | class |
+-------+-----+-------+
| Jacob |  1  |  boy  |
+-------+-----+-------+

电子邮件输出:

在此处输入图像描述

但是当将它从电子邮件复制到记事本时,对齐会恢复正常

4

0 回答 0