| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | """Unpack a MIME message into a directory of files.""" | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | import email | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | import errno | 
					
						
							|  |  |  | import mimetypes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | from optparse import OptionParser | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |     parser = OptionParser(usage="""\
 | 
					
						
							|  |  |  | Unpack a MIME message into a directory of files. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Usage: %prog [options] msgfile | 
					
						
							|  |  |  | """)
 | 
					
						
							|  |  |  |     parser.add_option('-d', '--directory', | 
					
						
							|  |  |  |                       type='string', action='store', | 
					
						
							|  |  |  |                       help="""Unpack the MIME message into the named
 | 
					
						
							|  |  |  |                       directory, which will be created if it doesn't already | 
					
						
							|  |  |  |                       exist.""")
 | 
					
						
							|  |  |  |     opts, args = parser.parse_args() | 
					
						
							|  |  |  |     if not opts.directory: | 
					
						
							|  |  |  |         parser.print_help() | 
					
						
							|  |  |  |         sys.exit(1) | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         msgfile = args[0] | 
					
						
							|  |  |  |     except IndexError: | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |         parser.print_help() | 
					
						
							|  |  |  |         sys.exit(1) | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |         os.mkdir(opts.directory) | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  |     except OSError, e: | 
					
						
							|  |  |  |         # Ignore directory exists error | 
					
						
							| 
									
										
										
										
											2006-08-29 04:39:12 +00:00
										 |  |  |         if e.errno != errno.EEXIST: | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |             raise | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fp = open(msgfile) | 
					
						
							|  |  |  |     msg = email.message_from_file(fp) | 
					
						
							|  |  |  |     fp.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     counter = 1 | 
					
						
							|  |  |  |     for part in msg.walk(): | 
					
						
							|  |  |  |         # multipart/* are just containers | 
					
						
							|  |  |  |         if part.get_content_maintype() == 'multipart': | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         # Applications should really sanitize the given filename so that an | 
					
						
							|  |  |  |         # email message can't be used to overwrite important files | 
					
						
							|  |  |  |         filename = part.get_filename() | 
					
						
							|  |  |  |         if not filename: | 
					
						
							|  |  |  |             ext = mimetypes.guess_extension(part.get_type()) | 
					
						
							|  |  |  |             if not ext: | 
					
						
							|  |  |  |                 # Use a generic bag-of-bits extension | 
					
						
							|  |  |  |                 ext = '.bin' | 
					
						
							|  |  |  |             filename = 'part-%03d%s' % (counter, ext) | 
					
						
							|  |  |  |         counter += 1 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |         fp = open(os.path.join(opts.directory, filename), 'wb') | 
					
						
							|  |  |  |         fp.write(part.get_payload(decode=True)) | 
					
						
							| 
									
										
										
										
											2002-10-01 14:17:10 +00:00
										 |  |  |         fp.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |