Beautiful table alignment out when sending table with email

1.6k views Asked by At

My table is printing fine in the console but when I email its alignment gets distorted a bit, any suggestion how to send it perfectly, as its printing in console?

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 = "[email protected]"
    toaddr = "[email protected]"
    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)

Console output:

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

Email output:

enter image description here

But when copy it from email to notepad alignments get back to normal

0

There are 0 answers