Emailing csv attachments from Google App Engine without downloading the attachments - python

I found this thread: GAE Python - How to attach the results of csv.writer to an email?
which has been helpful in getting me to be able to attach csv's to an email from Google App Engine. My code is as follows:
self.response.headers[str('Content-Type')] = str('text/csv')
self.response.headers[str('Content-Disposition')] = str('attachment; filename="data.csv"')
writer = csv.writer(self.response.out)
[write csv here]
message=mail.EmailMessage(sender='test#example.com',subject='Subject', attachments=[("data.csv",self.response.body)])
message.to=[to email here]
message.html='Message Body'
message.send()
The issue is when this code runs, not only is an email sent but a file is also downloaded. Is there a way to prevent the file from being downloaded?

It is downloading because you are writing it to the response output. You should be writing the attachment content to a temporary file/memory and attach that.
Also there is no need to set those headers.
The accepted answer to the SO question you have referenced is incorrect, the other answer is best https://stackoverflow.com/a/15453323/2018227

Related

Save email as .eml file with python

I am trying to export an Email message obtained with aiosmtpd to an *.eml file that I can later open with any *.eml viewer or email client like Thunderbird or Mail.
async def handle_DATA(self, server, session, envelope) -> str:
msg: EmailMessage = message_from_bytes(envelope.content, policy=policy.SMTPUTF8)
with open("test.eml", 'wb') as outfile:
outfile.write(msg.as_bytes())
I've also tried to save the file with Generator with both unixfrom=False and unixfrom=False and same thing.
with open("test.eml", 'w') as file:
emlGenerator = generator.Generator(file)
emlGenerator.flatten(msg, unixfrom=False)
The file gets created correctly but not all the eml files can be read correctly by Mail or Thunderbird.
Emails received from gmail.com are created correctly but emails received from protonmail.com are not. Eml files created from Protonmail emails can be opened but only from and to parameters of the email can be seen. I can't see the content neither the attachments in it.
I guess it does not have the format that eml parsers are expecting to see.
I've tried with different policies (like policy.SMTP, policy.default, ...).
What is the proper way to create eml files with python?
NOTE: The content of the EML file for Protonmail contains the following This is an OpenPGP/MIME signed message (RFC 4880 and 3156). Could this be related with the lack of correct parsing for Protonmail emails?
I had the same issue with the Email API. Although it can not always be opened directly as an .eml file, it is correctly opened if the message is sent to an SMTP server. I understand the Email API generates a string that represents the data sent to the SMTP server. That data is to be interpreted by an SMTP server, not a client. The SMTP server will interpret this data and store it as if finds more convenient (i.e. storing it in a db).
A client like Thunderbird will retrieve the message using POP3 or IMAP, but that doesn't mean that it is retrieving the same message that was sent to the SMTP server, since the server might store it in a different format.

How to save uploaded file from HTTP POST into FTP using Python?

I'm very very new to python. I have a HTML code with POST method to upload a file, then I have python code to receive the file and save it to a FTP server. My python code so far is :
berkas = request.FILES['file']
filename = "test_send_file.jpg"
upload_folder = '/var/www/uploads/target_folder/'
handle_uploaded_file(join(upload_folder, filename), berkas)
Using those codes I got Permission Denied when uploading the file.
I know the missing part is where I have to set FTP server address and it's username & password, but I don't know how to that.
So, how to do it ?
Or, is there easier way to solve my "POST upload then save to FTP" problem ?
Thank you before.

GAE Receive Email and Parse it

I am wondering whether Flask app in Google App Engine Production server can receive email with attachment from gmail or any other third party.
As I am having a requirement now that I would receive a email with csv attachment, need to parse it - take the decision and change the content on the website.
I am thinking of automating the entire thing, but I do no how to setup mail with google app engine, that is my application whenever receive a mail with attachment, parse it, do the computations and change the page content and display on the website.
I have gone through google app engine document but it doesn't provide much information.
I have setup the inbound_email in app.yaml and also handlers for the email, now the only question is how to get the email to the handler script. enter link description here
Is there any way on doing the above requirements ???

Integration of Python flask server and a File system

I am doing a project. It is using a File system(1GB File) rather than a database. I created the front end using angular. I wrote a Python-Flask-Server that handles the requests from the client( Browser) and dispatches the respective html files. However, I want to save all the users( Username and Password) regestered in a 1GB file for server side storage. Where should I start now?. Any resources ? Please help.

Attachment Sent to Kindle From Google Appengine not being Recognized

Using the code below I am trying to send an attachment to my Kindle from an Appengine App.
from google.appengine.api import mail
import cStringIO
out=cStringIO.StringIO()
out.write('Hello, World!')
reading = mail.EmailMessage(
sender='[sender email]',
subject='convert',
to=[list or recipient emails],
attachments=[('test.txt', out.getvalue())])
reading.send()
However, Amazon replies with
Your email to Kindle(s) did not include any attachments
Next I tried adding my email to the list of recipients and I get the email with the attachment just fine.
How come Amazon doesn't?
You're trying to send a .TXT file, but that's not a supported file type.
From Amazon's documentation:
Kindle Personal Documents Service supports multiple file types, including:
Microsoft Word (.DOC, .DOCX)
HTML (.HTML, .HTM)
RTF (.RTF)
JPEG (.JPEG, .JPG)
Kindle Format (.MOBI, .AZW)
GIF (.GIF)
PNG (.PNG)
BMP (.BMP)
PDF (.PDF)

Categories