How to send a zip file as an attachment in python?

I looked through a lot of manuals, as well as another question here about stack overflow, and the documentation and explanation is at least just inexplicable code. I would like to send the file that I already have and send it as an attachment. I tried to copy and paste the code, but it does not work, so I can not fix this problem.

So I ask if anyone can find out who will explain how smtplib, as well as the email and MIME libraries work together to send a file, or rather how to do it using a zip file. Any help would be appreciated.

This is the code everyone is referring to:

import smtplib import zipfile import tempfile from email import encoders from email.message import Message from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart def send_file_zipped(the_file, recipients, sender=' you@you.com '): myzip = zipfile.ZipFile('file.zip', 'w') # Create the message themsg = MIMEMultipart() themsg['Subject'] = 'File %s' % the_file themsg['To'] = ', '.join(recipients) themsg['From'] = sender themsg.preamble = 'I am not using a MIME-aware mail reader.\n' msg = MIMEBase('application', 'zip') msg.set_payload(zf.read()) encoders.encode_base64(msg) msg.add_header('Content-Disposition', 'attachment', filename=the_file + '.zip') themsg.attach(msg) themsg = themsg.as_string() # send the message smtp = smtplib.SMTP() smtp.connect() smtp.sendmail(sender, recipients, themsg) smtp.close() 

I suspect the problem is that this code also zips up the file. I do not want to be silent, because I already have a zip file that I would like to send. In any case, this code is poorly documented, as well as the python libraries themselves, since they do not give any information about any past img files or text files.

UPDATE: The error I am getting now. I also updated what is in my file with the code above

 Traceback (most recent call last): File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 100, in <module> send_file_zipped('hw5.zip', ' avaldez@oswego.edu ') File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 32, in send_file_zipped msg.set_payload(myzip.read()) TypeError: read() takes at least 2 arguments (1 given) 
+7
source share
2 answers

I really don't see the problem. Just omit the part that creates the zip file and instead just upload the zip file that you have.

Essentially this part here

 msg = MIMEBase('application', 'zip') msg.set_payload(zf.read()) encoders.encode_base64(msg) msg.add_header('Content-Disposition', 'attachment', filename=the_file + '.zip') themsg.attach(msg) 

creates an attachment.

 msg.set_payload(zf.read()) 

sets, well, the payload of the attachment to what you read from the zf file (perhaps this means the zip file).

Just open your zip file in advance and let this line be read.

+8
source

I agree that the email package is not yet documented. I explored it before and wrote a wrapper module that simplifies these tasks. For example, the following works:

 from pycopia import ezmail # Get the data data = open("/usr/lib64/python2.7/test/zipdir.zip").read() # Make a proper mime message object. zipattachement = ezmail.MIMEApplication.MIMEApplication(data, "zip", filename="zipdir.zip") # send it. ezmail.ezmail(["Here is the zip file.", zipattachement], To=" me@mydomain.com ", From=" me@mydomain.com ", subject="zip send test") 

And that’s all you need when everything is installed and configured. :-)

0
source

All Articles