-1

I wrote a program that sends me a calculated variable via gmail. However, I only see a couple of rows from the whole list out of the 147 rows which should be seen[this is what I get 1 but I would like to see every row.

I've found a similar problem: Output data from all columns in a dataframe in pandas but it only displays properly in the shell. When I tried these methods mentioned in the link, I've got a message which contained only: "None" or a completely blank message. I have no idea why it isn't working.

My code:

import pandas as pd
import smtplib
import tabulate
from io import StringIO

colnames = ['Hatarmetszek', 'Ora', 'diff']
data = pd.read_csv('calculatedvalue.csv', names=colnames)



gmail_user='mvm.napiadat@gmail.com'
gmail_password='password'
print (data) #modification work here

msg = "\r\n".join([
"From: user_me@gmail.com",
"To: user_you@gmail.com",
"Subject: napi adatok",
"",
str(data) # but don't here, this is the original one, which sends me the picture
])

sent_from='mvm.napiadat@gmail.com'
to=['bnyakas@mvmp.hu']





server=smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user,gmail_password)
server.sendmail(sent_from,to,msg)
server.quit()

Any help is appreciated.

Thank you.

nyaki
  • 45
  • 1
  • 2
  • 8
  • Did you try just printing the message (`print msg`) instead of sending it via email. When you do that, you will know whether the problem is in sending email or generating the string. This may solve your problem. If ti does not, you can modify the question, so that it includes only `msg` generation or only email sending. – zvone Sep 06 '18 at 07:24
  • I've tried it now, but I got the same thing as in email. Does it mean that the problem is in the generation? – nyaki Sep 06 '18 at 07:30
  • It means that `msg` variable is wrong even without sending email, so you should remove all code related to email from the question. Then, you should check whether `print(str(data)))` already print wrong data. If so, remove also the code related to creating `msg` and in the end, you'll end up asking about either: why `str` of results of `pd.read_csv` gives unexpected results, or about why there is difference between `str(data)` alone and `str(data)` within `msg`. At that point, you may even be able too google the answer ;) – zvone Sep 06 '18 at 07:38
  • Thank you. I'll try to trace it down. – nyaki Sep 06 '18 at 07:40

1 Answers1

-1

str(data) is not the right way of converting your dataframe to string representation.
str(data) dumps newline(\n) and carriage return(\r) characters into the mail body which messes up the email rendering.

Use body = data.to_html() to get your dataframe in html rows format. Send this body as the email content.

Ref: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html#pandas-dataframe-to-html

You will also have to do an additional change to send html instead of plain text as email body.

from email.mime.text import MIMEText

msg = MIMEText(data.to_html(), 'html')
msg['From'] = 'from email id'
msg['To'] = 'to email id'
msg['Subject'] = subject

....
....
server.sendmail(sent_from, to, msg.as_string())
Shiva
  • 2,627
  • 21
  • 33
  • Useful info, but I think it does not apply. The email message constructed in the question is plain text, not html. Sending html as plain text would just render html source code to the recipient. – zvone Sep 06 '18 at 08:42
  • Agreed. I missed that earlier. – Shiva Sep 06 '18 at 08:56