Create a text file and email it without saving it locally


user3558939:

I want to create a text file in Python, write something to it, and email it out ( test.txtas an email attachment).

However, I cannot save this file locally. Does anyone know how to do this?

As soon as the text file is opened for writing, the file is saved on the local computer.

f = open("test.txt","w+")

I am using smtpliband MIMEMultipartsending mail.

stacksonstacks:

StringIO is the way to go...

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart

from io import StringIO

email = MIMEMultipart()
email['Subject'] = 'subject'
email['To'] = '[email protected]'
email['From'] = '[email protected]'

# Add the attachment to the message
f = StringIO()
# write some content to 'f'
f.write("content for 'test.txt'")
f.seek(0)

msg = MIMEBase('application', "octet-stream")
msg.set_payload(f.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition',
               'attachment',
               filename='test.txt')
email.attach(msg)

Related


Create a CSV file without saving it to the file system

John I have created a Java program to create a csvfile, write data to it, and send its content to the server. Locally, everything works fine. But the problem is that I don't have write access to the server (permission denied issue). So, I can't do anything chm

Create a CSV file without saving it to the file system

John I have created a Java program to create a csvfile, write data to it, and send its content to the server. Locally, everything works fine. But the problem is that I don't have write access to the server (permission denied issue). So, I can't do anything chm

Create a CSV file without saving it to the file system

John I have created a Java program to create a csvfile, write data to it, and send its content to the server. Locally, everything works fine. But the problem is that I don't have write access to the server (permission denied issue). So, I can't do anything chm

Create a CSV file without saving it to the file system

John I have created a Java program to create a csvfile, write data to it, and send its content to the server. Locally, everything works fine. But the problem is that I don't have write access to the server (permission denied issue). So, I can't do anything chm

Attach file to email without saving content to local file

Shulamit Chatamov I am using Elixir Bamboo to send emails I have some binary content that I want to attach to an email The easy way is: content = <<binary-content>> File.write("/tmp/myfile.pdf", content ) data = new_email() |> to(email) |> from( "email.com

Use SendMessage to email file attachments without saving the file

soul I can send the email and everything, but can't create a valid Attachment() to put into the email. All the examples I've found online assume it's somehow saved on the local machine and linked by path, but that's not the case. In my method, I use Winnovativ

Use SendMessage to email file attachments without saving the file

soul I can send the email and everything, but can't create a valid Attachment() to put into the email. All the examples I've found online assume that the file is somehow saved on the local machine and linked by path, but that's not the case. In my method, I us

Attach file to email without saving content to local file

Shulamit Chatamov I am using Elixir Bamboo to send emails I have some binary content that I want to attach to an email The easy way is: content = <<binary-content>> File.write("/tmp/myfile.pdf", content ) data = new_email() |> to(email) |> from( "email.com

Use SendMessage to email file attachments without saving the file

soul I can send the email and everything, but can't create a valid Attachment() to put into the email. All the examples I've found online assume that the file is somehow saved on the local machine and linked by path, but that's not the case. In my method, I us

Write text file in memory without saving to disk

bart van der drift I have a list of strings as input that I need to put into a text file which is then put into an encrypted ZIP archive. I'm going to do it in two steps: Create a text file and get its bytes Write bytes to archive using zip library However, th

Parse text file in Ruby on Rails without saving it

Simon Means I have a text file of the form texttexttexttexttexttexttexttexttext texttexttexttexttexttexttexttexttext texttexttexttexttexttexttexttexttext texttexttexttexttexttexttexttexttext >>>> texttexttexttexttexttexttexttexttext texttexttexttexttexttexttex

Write text file in memory without saving to disk

bart van der drift I have a list of strings as input that I need to put into a text file which is then put into an encrypted ZIP archive. I'm going to do it in two steps: Create a text file and get its bytes Write bytes to archive using zip library However, th

Parse text file in Ruby on Rails without saving it

Simon Means I have a text file of the form texttexttexttexttexttexttexttexttext texttexttexttexttexttexttexttexttext texttexttexttexttexttexttexttexttext texttexttexttexttexttexttexttexttext >>>> texttexttexttexttexttexttexttexttext texttexttexttexttexttexttex

OpenXML - create and open Excel file without saving it

Lucy 82 I have a code that uses OpenXML to create a new Excel file from Datatable. After I finish creating this file, I want to open it for the user without saving it. Basically what Excel does in this case is open the excel file as "Workbook1" and then save i

OpenXML - create and open Excel file without saving it

Lucy 82 I have a code that uses OpenXML to create a new Excel file from Datatable. After I finish creating this file, I want to open it for the user without saving it. Basically what Excel does in this case is open the excel file as "Workbook1" and then save i