88# For guessing MIME type based on file name extension
99import mimetypes
1010
11- from optparse import OptionParser
11+ from argparse import ArgumentParser
1212
1313from email import encoders
1414from email .message import Message
2222
2323
2424def main ():
25- parser = OptionParser ( usage = """\
25+ parser = ArgumentParser ( description = """\
2626 Send the contents of a directory as a MIME message.
27-
28- Usage: %prog [options]
29-
3027Unless the -o option is given, the email is sent by forwarding to your local
3128SMTP server, which then does the normal delivery process. Your local machine
3229must be running an SMTP server.
3330""" )
34- parser .add_option ('-d' , '--directory' ,
35- type = 'string' , action = 'store' ,
36- help = """Mail the contents of the specified directory,
37- otherwise use the current directory. Only the regular
38- files in the directory are sent, and we don't recurse to
39- subdirectories.""" )
40- parser .add_option ('-o' , '--output' ,
41- type = 'string' , action = 'store' , metavar = 'FILE' ,
42- help = """Print the composed message to FILE instead of
43- sending the message to the SMTP server.""" )
44- parser .add_option ('-s' , '--sender' ,
45- type = 'string' , action = 'store' , metavar = 'SENDER' ,
46- help = 'The value of the From: header (required)' )
47- parser .add_option ('-r' , '--recipient' ,
48- type = 'string' , action = 'append' , metavar = 'RECIPIENT' ,
49- default = [], dest = 'recipients' ,
50- help = 'A To: header value (at least one required)' )
51- opts , args = parser .parse_args ()
52- if not opts .sender or not opts .recipients :
53- parser .print_help ()
54- sys .exit (1 )
55- directory = opts .directory
31+ parser .add_argument ('-d' , '--directory' ,
32+ help = """Mail the contents of the specified directory,
33+ otherwise use the current directory. Only the regular
34+ files in the directory are sent, and we don't recurse to
35+ subdirectories.""" )
36+ parser .add_argument ('-o' , '--output' ,
37+ metavar = 'FILE' ,
38+ help = """Print the composed message to FILE instead of
39+ sending the message to the SMTP server.""" )
40+ parser .add_argument ('-s' , '--sender' , required = True ,
41+ help = 'The value of the From: header (required)' )
42+ parser .add_argument ('-r' , '--recipient' , required = True ,
43+ action = 'append' , metavar = 'RECIPIENT' ,
44+ default = [], dest = 'recipients' ,
45+ help = 'A To: header value (at least one required)' )
46+ args = parser .parse_args ()
47+ directory = args .directory
5648 if not directory :
5749 directory = '.'
5850 # Create the enclosing (outer) message
5951 outer = MIMEMultipart ()
6052 outer ['Subject' ] = 'Contents of directory %s' % os .path .abspath (directory )
61- outer ['To' ] = COMMASPACE .join (opts .recipients )
62- outer ['From' ] = opts .sender
53+ outer ['To' ] = COMMASPACE .join (args .recipients )
54+ outer ['From' ] = args .sender
6355 outer .preamble = 'You will not see this in a MIME-aware mail reader.\n '
6456
6557 for filename in os .listdir (directory ):
@@ -76,38 +68,32 @@ def main():
7668 ctype = 'application/octet-stream'
7769 maintype , subtype = ctype .split ('/' , 1 )
7870 if maintype == 'text' :
79- fp = open (path )
80- # Note: we should handle calculating the charset
81- msg = MIMEText (fp .read (), _subtype = subtype )
82- fp .close ()
71+ with open (path ) as fp :
72+ # Note: we should handle calculating the charset
73+ msg = MIMEText (fp .read (), _subtype = subtype )
8374 elif maintype == 'image' :
84- fp = open (path , 'rb' )
85- msg = MIMEImage (fp .read (), _subtype = subtype )
86- fp .close ()
75+ with open (path , 'rb' ) as fp :
76+ msg = MIMEImage (fp .read (), _subtype = subtype )
8777 elif maintype == 'audio' :
88- fp = open (path , 'rb' )
89- msg = MIMEAudio (fp .read (), _subtype = subtype )
90- fp .close ()
78+ with open (path , 'rb' ) as fp :
79+ msg = MIMEAudio (fp .read (), _subtype = subtype )
9180 else :
92- fp = open (path , 'rb' )
93- msg = MIMEBase (maintype , subtype )
94- msg .set_payload (fp .read ())
95- fp .close ()
81+ with open (path , 'rb' ) as fp :
82+ msg = MIMEBase (maintype , subtype )
83+ msg .set_payload (fp .read ())
9684 # Encode the payload using Base64
9785 encoders .encode_base64 (msg )
9886 # Set the filename parameter
9987 msg .add_header ('Content-Disposition' , 'attachment' , filename = filename )
10088 outer .attach (msg )
10189 # Now send or store the message
10290 composed = outer .as_string ()
103- if opts .output :
104- fp = open (opts .output , 'w' )
105- fp .write (composed )
106- fp .close ()
91+ if args .output :
92+ with open (args .output , 'w' ) as fp :
93+ fp .write (composed )
10794 else :
108- s = smtplib .SMTP ('localhost' )
109- s .sendmail (opts .sender , opts .recipients , composed )
110- s .quit ()
95+ with smtplib .SMTP ('localhost' ) as s :
96+ s .sendmail (args .sender , args .recipients , composed )
11197
11298
11399if __name__ == '__main__' :
0 commit comments