0

I'm working on an automated email sender script. My goal is to send 2 tables by email each and every time my results are generated. So far I tried the tabulate python package, which looks really nice if I print it in the console of PyCharm, but when I send this format by email, the look breaks apart.

pycharm output: pycharm output

Email output: Email output

For this I used a method found here: Send table as an email body (not attachment ) in Python

My aim is to achive a similar table in outlook, like in pycharm. What I noticed is that the rows of data is being split when it appears in outlook.

UPDATE:

Here is the code I have tryed:

import pandas as pd
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sample_df = pd.read_csv("sampleqc.txt", sep="\t")
sample_df = sample_df.to_html(table_id='SAMPLEQC', index=False)
sample_df = sample_df.replace('class="dataframe" id="SAMPLEQC">', 'class="styled-table" id="SAMPLEQC">')
with open("table_style.txt") as style:
    table_style_html = style.read()

final_html = "<html><body>" + table_style_html + sample_df + "</body></html>"
me = 'mymail@gmail.com'
password = 'password'
server = 'smtp.gmail.com:587'
you = 'custom@domain.mail'

message = MIMEMultipart("alternative")
message['Subject'] = "Your data"
message['From'] = me
message['To'] = you2

html_body = MIMEText(final_html, 'html')
message.attach(html_body)

server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you2, message.as_string())
server.quit()

I tried to use it to send email to Gmail which look like this: enter image description here

and the same code in Microsoft Outlook: enter image description here

Any idea how can I manage this problem? Thanks in advance!

pahi
  • 95
  • 5

1 Answers1

0

Use Pandas to store the table's data (e.g. someTableDataFrame = pandas.read_csv("some.csv") or someTableDataFrame = pandas.DataFrame(someListOrArray)), and either the .to_html() method on its own (e.g. someTableDataFrame.to_html() or with the Pandas styler (e.g. df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])]).hide(axis='index').to_html()) For guidance on sending HTML emails, see here Send HTML emails with Python

James_SO
  • 1,169
  • 5
  • 11
  • Hi! Thanks for your answer, but the this does not solve the problem sadly. When I try to send a HTML email in Gmail it appears properly, however in Microsoft Outlook it still breaks apart. – pahi Jul 05 '23 at 07:26