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! :)