Reputation: 87
I read my data from mongo dB and create a report. I used to save the report as a .xlsx file on my local machine and manually send it using email. I would like to automate this process.
I found a way in Python 3 to save the file in-memory using Python's io.BytesIO() functionality. I tried a few things that I found on stack overflow but it didn't work for me. I would like to get some suggestions on how I can automate my work.
Following is what I have tried,
df = pd.dataframe(df) # has my reports dataframe
def export_excel(df):
with io.BytesIO() as buffer:
writer = pd.ExcelWriter(buffer)
df.to_excel(writer)
writer.save()
return buffer.getvalue()
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
SEND_FROM = '[email protected]'
EXPORTERS = {'dataframe.csv': export_csv, 'dataframe.xlsx': export_excel}
def send_dataframe(send_to, subject, body, df):
multipart = MIMEMultipart()
multipart['From'] = SEND_FROM
multipart['To'] = '[email protected]'
multipart['Subject'] = subject
for filename in EXPORTERS:
attachment = MIMEApplication(EXPORTERS[filename](df))
attachment['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
multipart.attach(attachment)
multipart.attach(MIMEText(body, 'html'))
s = smtplib.SMTP('localhost')
s.sendmail(SEND_FROM, send_to, multipart.as_string())
s.quit()
I do not get an error but don't get an email with an excel attachment too.
Upvotes: 2
Views: 2487
Reputation: 1
Here is a snippet of my code where I was taking a dataframe then convert it to StringIO() to send it as a spreadsheet format. Only difference is I am sending a csv file instead of xlsx. Hope it helps.
textStream = StringIO()
dataframe.to_csv(textStream,index=False)
message = MIMEMultipart()
message['Subject'] = "Given Subject"
message.attach(MIMEApplication(textStream.getvalue(), Name="Name of the
file"))
context = ssl.create_default_context()
with smtplib.SMTP(SMTP_host, SMTP_port) as server:
server.starttls(context=context)
server.login("mail user", "mail password")
server.sendmail("mail user", "receiver email", message.as_string().encode('utf-8'))
server.close()
Upvotes: 0
Reputation: 342
I need to solve the same topic, so here is my code:
import io
import pandas as pd
from pandas import util
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
df = util.testing.makeDataFrame()
# df.head()
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
output.seek(0)
send_from = '[email protected]'
gmail_pwd = 'my_gmail_password'
send_to = '[email protected]'
subject = 'Daily report'
body = "<p>Please find attached report,<br/>by me</p>"
report_name = "report.xlsx"
msg = MIMEMultipart()
msg['Subject'] = subject # add in the subject
msg['From'] = send_from
msg['To'] = send_to
msg.attach(MIMEText(body, 'html')) # add text contents
file = MIMEApplication(output.read(), name=report_name)
file['Content-Disposition'] = f'attachment; filename="{report_name}"'
msg.attach(file)
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.ehlo()
smtp.starttls()
smtp.login(send_from, gmail_pwd)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()
Upvotes: 2