Sending a in-memory csv attachment using smtplib - python

I have a pandas dataframe "df" in the code. I want to be able to send df.to_csv('filename') as an attachment in the email.
How do I do that? I dont want to write the file in a temp location and then attach that in the email. Is there a way I could attach the csv within in-memory?

Related

Python Email Saving and Attachment Within An Attachment

As part of one of the processes we run we receive an email (in Outlook) that has a csv attachment within a .msg attachment that we need to save down.
I'm trying to save the .csv file but cannot figure out how to get to the attachment in the attachment. I am also brand new to Python so go easy on me. I have made the query generic but obviously the below only saves down the first attachment - I tried creating a variable for att.Attachment but that didn't work. HELP!
So far I have this:
import win32com.client
from datetime import datetime
from pathlib import Path
import csv
import os
outlook = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages=inbox.Items
messages.Sort("[ReceivedTime]", True)
for i in list(messages):
if "Hello" in str(i.subject):
for att in i.Attachments:
att.SaveAsFile(r'/C:/Users/el/Documents/' + att.FileName)
The Outlook object model doesn't provide any methods for processing attached files beside saving them to the disk. So, you can save attached files to the disk by using the Attachment.SaveAsFile method. Then you you can open the saved files by automating their target applications, for example, the .msg file can be run to be opened in the default application (Outlook). In that case you will get a new Inspector window opened because Outlook is singleton, only one application can be run at the same time on the system. For the .csv file Excel can be automated to deal with a file content.

How to Convert email from MBOX to EML using Python

Is there a way to convert some MBOX files to individual EML files using Python?
I have a client that downloaded his emails from GMail as MBOX collection. But I need to convert them to individual EML files so I can upload them to his new email client/server - which does not accept MBOX files.
Thanks in advance for any feedback.
UPDATE:
I have no code yet. I am wondering if it's even possible to do this via a small app, if not I'll purchase one of those available online. Just thought it would be cool to try it myself first.
Also, from my understanding, an mbox is a collection of emails (.eml files) and a single email would be just an .eml file.

export dataframe excel directly to sharepoint (or a web page)

I created a dataframe to jupyter notebook and I would like to export this dataframe directly to sharepoint as an excel file. Is there a way to do that?
As per the other answers, there doesn't seem to be a way to write a dataframe directly to SharePoint at the moment, but rather than saving it to file locally, you can create a BytesIO object in memory, and write that to SharePoint using the Office365-REST-Python-Client.
from io import BytesIO
buffer = BytesIO() # Create a buffer object
df.to_excel(buffer, index=False) # Write the dataframe to the buffer
buffer.seek(0)
file_content = buffer.read()
Referencing the example script upload_file.py, you would just replace the lines reading in the file with the code above.
You can add the SharePoint site as a network drive on your local computer and then use a file path that way. Here's a link to show you how to map the SharePoint to a network drive. From there, just select the file path for the drive you selected.
df.to_excel(r'Y:\Shared Documents\file_name.xlsx')
I think there is no way directly export pandas dataframe data to sharepoint but you can export your pandas data to excel and then upload the excel data to sharepoint
Export Covid data to Excel file
df.to_excel(r'/home/noh/Desktop/covid19.xls', index=False)

Sending a csv file's content as a table in a mail from python

I have a csv file . Want to send the content of this file as a table through email from python. I am working in unix environment.
Thanks in advance
Edit:
Found a way!
Was writing the CSV file from a dataframe object.
Converted the df to html table using "to_html" and send using sendmail().
Is there any better way ?

Encrypt a file with a password to send via email in python

Here is what I need to do in python:
Take a CSV from a saved location. Encrypt it. Send it via email.
We have a report that creates a csv every week and it needs to get emailed to someone. When they open the attachment from their email, it needs to ask them to input a predetermined password before they can view the csv contents.
I know how to send email with attachments using the smtp and mime modules.
I don't really know much about encryption but most of my searches turn up for files that are going to also be DEcrypted by python, or for encrypting passwords themselves.
Is asking for password upon opening a .csv file possible? Are there any resources out there for tutorials on this?
As an unsophisticated approach, you could zip the csv file that requires a password to unzip. This is a simple command line option (to the zip utility) that you could call from Python, then attach the resulting file. This wouldn't give you the best encryption, but may serve your purpose. See this link on using stronger encryption with zip archives.

Categories