1

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 = "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)

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ahsan Naseem
  • 1,046
  • 1
  • 19
  • 38
  • 3
    The email is simply not written into a monospaced font... https://en.wikipedia.org/wiki/Monospaced_font – Willem Van Onsem Jul 25 '17 at 19:34
  • Consider either creating an html table with your data instead, or adding a monospaced font style definition in the html portion of the message. To send an html email with smtplib see https://stackoverflow.com/questions/882712/sending-html-email-using-python – TheoretiCAL Jul 25 '17 at 19:49

0 Answers0