I'm trying to send a yagmail email containing a pandas DataFrame using the method detailed in this post: Python Email in HTML format mimelib .
However, no matter what I try yagmail adds a long string of line breaks prior to the DataFrame being printed.
My code is (I only changed the email addresses for my installation):
#--- Addition to original post ---
import pandas as pd
df = pd.DataFrame([[900, 20.0], [500, 21.0]], columns =['Quantity', 'Price'])
#--- Addition to original post ---
import time
import yagmail
yag = yagmail.SMTP(username, password)
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = df.to_html()
yag.send('albalb@gmail.com', "This a reminder call " + time.strftime("%c"), [text,html])
The resulting email body is:
>Hi!
>How are you?
>Here is the link you wanted:
>https://www.python.org
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Quantity Price
>0 900 20.0
>1 500 21.0
I've also tried adding a DOCTYPE tag, encoding as utf-8, converting from unicode to string but I've had no success.
Is this a bug, or can anyone help me with a solution?
Edit #1: Please note that the html variable contains the following:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>900</td>
<td>20.0</td>
</tr>
<tr>
<th>1</th>
<td>500</td>
<td>21.0</td>
</tr>
</tbody>
</table>
Edit #2: To elaborate on my attempting to add html and doctype tags, I tried adding the following at the beginning and end of html.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Title</title>
</head>
<body>
...
</body>
</html>
I validated this was valid html here and the resulting email body was similar.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Quantity Price
0 900 20.0
1 500 21.0
` tags are added. I'm clueless. – PascalVKooten Mar 24 '17 at 21:56