I'm having trouble sending my DataFrame through an e-mail. The DataFrame looks fine whenever I export it or print it. Through an e-mail however, it looks messed up.
Of course, I've tried the to_html
option that Pandas offers. And also the build_table
from the pretty_html_table
package.
Whenever I send my dataFrame through an e-mail, it returns this:
Dear mister X,
Please have a look at these numbers. There is a big change in revenue since 11-10-2021 compared to 10-10-2021:
<p><table border="0" class="dataframe">
<thead>
<tr style="text-align: right;">
<th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Product Category</th>
<th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Source / Medium</th>
<th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Avg. Rev. Period 1</th>
<th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Avg. Rev. Period 2</th>
<th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Percentage Change</th>
</tr>
</thead>
<tbody>
<tr>
<td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">TEST</td>
<td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">TEST</td>
<td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">0.01</td>
<td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">100</td>
<td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">-52</td>
</tr>
.........
I think it probably has to do with the code I use to send the e-mail. I want to insert different variables in it. Here's the code for the e-mail:
email_df = build_table(df, "blue_light")
subject = f"Subject: blablabla - {today}"
message = f"\n\nDear mister X, \n\n Please have a look at these numbers. There is a big change in revenue since {period 1} compared to {period 2}: \n\n {email_df}"
emailcontent = f'{subject}{message}'
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, emailcontent)
Any help is welcome! :)
You are currently sending the email in plain text format, which makes it impossible to ensure the column spacing is correct unless the recipient happens to view their emails in a plain text reader.
To ensure the table is spaced correctly you have to send an HTML email - see the example here and this answer . Something like the following (untested) should work:
from email.message import EmailMessage
import smptlib
email = EmailMessage()
email['Subject'] = f"Subject: blablabla - {today}"
email['From'] = sender_email
email['To'] = receiver_email
email.set_content(f"""\
<html><head></head><body>
<p>Dear mister X,</p>
<p>Please have a look at these numbers. There is a big change in revenue since {period 1}
compared to {period 2}:</p>
{df.to_html()} # or use build_table(df) for prettier formatting
</body></html>""", subtype='html')
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.send_message(email)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.