1

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
CurryPy
  • 43
  • 5
  • what is the output when you change `[text,html]` to `[html,text]`. The reason I ask is to check if the df is the issue or if it is yagmail. Unfortunately, I see a lot of trial and error ahead! – MattR Mar 24 '17 at 13:39
  • @MattR Thank you for the suggestion. I reran the code replacing the last line with: `yag.send('albalb@gmail.com', "This a reminder call " + time.strftime("%c"), [html])`. The resulting email body contained a series of line breaks and then the html table at the bottom – CurryPy Mar 24 '17 at 13:58
  • yagmail's author here.... it's definitely a bug. I can see 5 `
    ` tags are added. I'm clueless.
    – PascalVKooten Mar 24 '17 at 21:56
  • @PascalvKooten thank you for looking at this for me. Do you want me to add this to a bug log somewhere? If so, can you post the link. – CurryPy Mar 25 '17 at 18:08
  • @CurryPy Thanks, I tried to find a temporary solution but couldn't find it. https://github.com/kootenpv/yagmail/issues – PascalVKooten Mar 25 '17 at 19:01
  • @PascalvKooten thank you for your efforts, the issue has been logged [here](https://github.com/kootenpv/yagmail/issues/68). I look forward to the next release. On behalf of yagmail users everywhere, thank you. – CurryPy Mar 26 '17 at 19:55

1 Answers1

0

Too long to comment: One thing that you can do is create a variable of the output before sending and delete the spaces manually. For example, in one of my emailer programs I built in the past i used easygui and used the textbox for users to create their message. Then send that message. I small example:

import easygui as e
text_mes= e.textbox("Add to the Email", "Create Your Email", html)
yag.send('albalb@gmail.com', "This a reminder call " + time.strftime("%c"), [text,text_mes])

This will allow you to manually delete the rows until you can find a way to create some code to get rid of the spaces.

MattR
  • 4,887
  • 9
  • 40
  • 67
  • thank you for taking the time to help me with this. I modified the post to show the html variable: it appears to be clean. So there is nothing to remove. The html variable is unicode, so i've tried converting to a string prior to yagmail and also adding .encode('utf-8'), but all variations produce in the same result. I've also tried removing the border and class identifiers from the table tag. – CurryPy Mar 24 '17 at 14:40