Hey guys,
I am making a small script here and I got stuck on it a bit ... perhaps one of you could help me out

The idea is to send an email that has attached zip file to it. My script is

import smtplib
import zipfile
import time
from email.mime.multipart import MIMEMultipart 
from email.mime.base import MIMEBase 
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate 
from email import encoders




def sendGmail(user = "hidden",pwd = "hidden",FROM = "hidden",\
TO = ['hidden'],SUBJECT = 'email',textMessage = 'Sample Massage'):
    #message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    #""" % (FROM, ", ".join(TO), SUBJECT, textMessage)

    zf = zipfile.ZipFile("file.zip")

    msg = MIMEMultipart()
    msg['From'] = FROM
    msg['To'] = TO
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = SUBJECT
    msg.attach (MIMEText(textMessage))

    part = MIMEBase('application', "octet-stream")
    part.set_payload(zf.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="file.zip"')
    msg.attach(part)






    try:
        server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
        server.ehlo()
        server.login(user, pwd)
        server.sendmail(FROM, TO, str(msg))
        server.close()
        print 'successfully sent the mail'
    except Exception, e:
        print e

def main():
    textMessage = '''
        Email from a script
        The information in here is
        a zip file.
    '''
    sendGmail(TO = [hidden'],textMessage = textMessage)

if __name__ == '__main__':
    main()

It works fine if I send only a message without attaching a file such as:

def sendGmail(user = "",pwd = "",FROM = "",\
TO = [''],SUBJECT = 'email',textMessage = 'Sample Massage'):
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, textMessage)


    try:
        server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
        server.ehlo()
        server.login(user, pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print 'successfully sent the mail'
    except Exception, e:
        print e

But when I tried sending a zip file I get error that read() takes at least 2 parameters

 File "emailAttachedZip.py", line 28, in sendGmail
    part.set_payload(zf.read())
TypeError: read() takes at least 2 arguments (1 given)

pymotw is good place to learn about using modules. Here is the lesson on zipfile:
http://pymotw.com/2/zipfile/

Also by example of the module you see that text of the message is input as preamble:

https://docs.python.org/2.7/library/email-examples.html

Also I believe that you should be reading the file as normal file (not zf) with read without parameters. The file would be previously zipped, so it would only be read as binary file and encoded with base64 (see last example end, else-part)

So change the zipfile part to normal open('file.zip', 'rb')

Do I need to supply a file that is part of that zip file in the read? :o

Like my purpose is to attach the entire zip file not files in it separately

File is file,just attach whole file. It is receiver who opens the content, it does not matter except for the mime type.

Hey Tony,
changed it to zf = open('file.zip','rb'), works great thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.