Back to index

python3.2  3.2.2
Functions | Variables
email-dir Namespace Reference

Functions

def main

Variables

string COMMASPACE = ', '

Function Documentation

def email-dir.main ( void  )

Definition at line 24 of file email-dir.py.

00024 
00025 def main():
00026     parser = OptionParser(usage="""\
00027 Send the contents of a directory as a MIME message.
00028 
00029 Usage: %prog [options]
00030 
00031 Unless the -o option is given, the email is sent by forwarding to your local
00032 SMTP server, which then does the normal delivery process.  Your local machine
00033 must be running an SMTP server.
00034 """)
00035     parser.add_option('-d', '--directory',
00036                       type='string', action='store',
00037                       help="""Mail the contents of the specified directory,
00038                       otherwise use the current directory.  Only the regular
00039                       files in the directory are sent, and we don't recurse to
00040                       subdirectories.""")
00041     parser.add_option('-o', '--output',
00042                       type='string', action='store', metavar='FILE',
00043                       help="""Print the composed message to FILE instead of
00044                       sending the message to the SMTP server.""")
00045     parser.add_option('-s', '--sender',
00046                       type='string', action='store', metavar='SENDER',
00047                       help='The value of the From: header (required)')
00048     parser.add_option('-r', '--recipient',
00049                       type='string', action='append', metavar='RECIPIENT',
00050                       default=[], dest='recipients',
00051                       help='A To: header value (at least one required)')
00052     opts, args = parser.parse_args()
00053     if not opts.sender or not opts.recipients:
00054         parser.print_help()
00055         sys.exit(1)
00056     directory = opts.directory
00057     if not directory:
00058         directory = '.'
00059     # Create the enclosing (outer) message
00060     outer = MIMEMultipart()
00061     outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
00062     outer['To'] = COMMASPACE.join(opts.recipients)
00063     outer['From'] = opts.sender
00064     outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
00065 
00066     for filename in os.listdir(directory):
00067         path = os.path.join(directory, filename)
00068         if not os.path.isfile(path):
00069             continue
00070         # Guess the content type based on the file's extension.  Encoding
00071         # will be ignored, although we should check for simple things like
00072         # gzip'd or compressed files.
00073         ctype, encoding = mimetypes.guess_type(path)
00074         if ctype is None or encoding is not None:
00075             # No guess could be made, or the file is encoded (compressed), so
00076             # use a generic bag-of-bits type.
00077             ctype = 'application/octet-stream'
00078         maintype, subtype = ctype.split('/', 1)
00079         if maintype == 'text':
00080             fp = open(path)
00081             # Note: we should handle calculating the charset
00082             msg = MIMEText(fp.read(), _subtype=subtype)
00083             fp.close()
00084         elif maintype == 'image':
00085             fp = open(path, 'rb')
00086             msg = MIMEImage(fp.read(), _subtype=subtype)
00087             fp.close()
00088         elif maintype == 'audio':
00089             fp = open(path, 'rb')
00090             msg = MIMEAudio(fp.read(), _subtype=subtype)
00091             fp.close()
00092         else:
00093             fp = open(path, 'rb')
00094             msg = MIMEBase(maintype, subtype)
00095             msg.set_payload(fp.read())
00096             fp.close()
00097             # Encode the payload using Base64
00098             encoders.encode_base64(msg)
00099         # Set the filename parameter
00100         msg.add_header('Content-Disposition', 'attachment', filename=filename)
00101         outer.attach(msg)
00102     # Now send or store the message
00103     composed = outer.as_string()
00104     if opts.output:
00105         fp = open(opts.output, 'w')
00106         fp.write(composed)
00107         fp.close()
00108     else:
00109         s = smtplib.SMTP('localhost')
00110         s.sendmail(opts.sender, opts.recipients, composed)
00111         s.quit()
00112 

Here is the call graph for this function:


Variable Documentation

string email-dir.COMMASPACE = ', '

Definition at line 21 of file email-dir.py.