[code.view]

[top] / python / PyMOTW / mailbox / mailbox_maildir_create.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import mailbox
     import email.utils
     import os
     
     from_addr = email.utils.formataddr(('Author', 'author@example.com'))
     to_addr = email.utils.formataddr(('Recipient', 'recipient@example.com'))
     
     mbox = mailbox.Maildir('Example')
     mbox.lock()
     try:
         msg = mailbox.mboxMessage()
         msg.set_unixfrom('author Sat Feb  7 01:05:34 2009')
         msg['From'] = from_addr
         msg['To'] = to_addr
         msg['Subject'] = 'Sample message 1'
         msg.set_payload('This is the body.\nFrom (will not be escaped).\nThere are 3 lines.\n')
         mbox.add(msg)
         mbox.flush()
     
         msg = mailbox.mboxMessage()
         msg.set_unixfrom('author Sat Feb  7 01:05:34 2009')
         msg['From'] = from_addr
         msg['To'] = to_addr
         msg['Subject'] = 'Sample message 2'
         msg.set_payload('This is the second body.\n')
         mbox.add(msg)
         mbox.flush()
     finally:
         mbox.unlock()
     
     for dirname, subdirs, files in os.walk('Example'):
         print dirname
         print '\tDirectories:', subdirs
         for name in files:
             fullname = os.path.join(dirname, name)
             print
             print '***', fullname
             print open(fullname).read()
             print '*' * 20

[top] / python / PyMOTW / mailbox / mailbox_maildir_create.py

contact | logmethods.com