-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy path5_3_email_current_dir_zipped.py
55 lines (50 loc) · 1.87 KB
/
5_3_email_current_dir_zipped.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# Python Network Programming Cookbook, Second Edition -- Chapter - 5
# This program is optimized for Python 2.7.12 and Python 3.5.2.
# It may run on any other version with/without modifications.
import os
import argparse
import smtplib
import zipfile
import tempfile
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
def email_dir_zipped(sender, recipient):
zf = tempfile.TemporaryFile(prefix='mail', suffix='.zip')
zip = zipfile.ZipFile(zf, 'w')
print ("Zipping current dir: %s" %os.getcwd())
for file_name in os.listdir(os.getcwd()):
zip.write(file_name)
zip.close()
zf.seek(0)
# Create the message
print ("Creating email message...")
email_msg = MIMEMultipart()
email_msg['Subject'] = 'File from path %s' %os.getcwd()
email_msg['To'] = ', '.join(recipient)
email_msg['From'] = sender
email_msg.preamble = 'Testing email from Python.\n'
msg = MIMEBase('application', 'zip')
msg.set_payload(zf.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',
filename=os.getcwd()[-1] + '.zip')
email_msg.attach(msg)
email_msg = email_msg.as_string()
# send the message
print ("Sending email message...")
try:
smtp = smtplib.SMTP('localhost')
smtp.set_debuglevel(1)
smtp.sendmail(sender, recipient, email_msg)
except Exception as e:
print ("Error: %s" %str(e))
finally:
smtp.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Email Example')
parser.add_argument('--sender', action="store", dest="sender", default='[email protected]')
parser.add_argument('--recipient', action="store", dest="recipient")
given_args = parser.parse_args()
email_dir_zipped(given_args.sender, given_args.recipient)