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)