28

My input file is a CSV file and by running some python script which consists of the python Tabulate module, I have created a table that looks like this below:-

tabulate_output or

|    Attenuation |   Avg Ping RTT in ms |   TCP UP |
|---------------:|---------------------:|---------:|
|             60 |                2.31  | 106.143  |
|             70 |                2.315 | 103.624  |

I would like to send the this table in the email body and not as an attachment using python.

I have created a sendMail function and will be expecting to send the table in the mail_body. sendMail([to_addr], from_addr, mail_subject, mail_body, [file_name])

Durvesh
  • 293
  • 1
  • 3
  • 6
  • You could ask the recipient to view mail with fixed width font and just put table in the email body. – joel goldstick Jul 08 '16 at 21:18
  • Why not send it as html table? – Arnial Jul 08 '16 at 21:22
  • You will need to construct the HTML string and send it across with proper Content-Type header. [An example here](https://github.com/supersaiyanmode/gapi/blob/master/GApi4Term/commands/email.py#L8). – UltraInstinct Jul 08 '16 at 21:24
  • I am already sending this whole table as an attachment in the text file. The client wants it in the body itself. @Arnial- Will the format remain the same with the HTML Table ? – Durvesh Jul 08 '16 at 21:37

1 Answers1

43

This code sends the message in the typical plain text plus HTML multipart/alternative format. If your correspondent reads this in an HTML-aware mail reader, he'll see the HTML table. If he reads it in a plain-text reader, he'll see the plain-text version.

In either case, he will see the data included in the body of the message, and not as an attachment.

import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

me = 'xxx@gmail.com'
password = 'yyyzzz!!2'
server = 'smtp.gmail.com:587'
you = 'qqq@gmail.com'

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('input.csv') as input_file:
    reader = csv.reader(input_file)
    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()
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • @Rob- Thnx a lot. Worked for me. This was the key line: message = MIMEMultipart( "alternative", None, [MIMEText(text), MIMEText(html,'html')]) – Durvesh Jul 12 '16 at 22:08
  • Hello Rob, I have used this piece of code with Python 2.6, Email seems to be working, HTML table is displaying organized data, but there are no borders around it to make it actually table visually. Could you please suggest what needs to be done here? – Venu S Oct 10 '17 at 15:39
  • @VenuS - I'm not sure how one specifies a board using the `tabulate` package. Perhaps you can build the HTML some other way and specify either the `border` attribute or use CSS. Regardless, you might want to open a new question on [SO] to see what others have to say. – Robᵩ Oct 10 '17 at 16:30
  • @VenuS - When I am trying to implement similar kind of scenario it shows the error : " ImportError: No module named tabulate ". However when I am trying to install tabulate, it doesn't allow me to do so. is there any alternative for this? – code Jun 29 '19 at 09:07
  • @code can you share the command you are trying to install tabulate package I believe you should be specifying -u flag with command, so it installs only for your user – Venu S Jul 01 '19 at 15:28
  • @VenuS- I am using python 2.7. and I am using pip install tabulate command – code Jul 01 '19 at 18:24
  • @Robᵩ - your code is helpful, upvoted. But I have similar problem, can you help me with the below post? https://stackoverflow.com/questions/71675034/unable-to-correctly-format-excel-table-to-outlook-body-using-python – The Great Mar 30 '22 at 09:50