示例#1
0
def LoadMailboxFromConfiguration(mailbox_type: str,
                                 location: str) -> mailbox.Mailbox:
    mail_box = None
    if os.path.exists(location) is True:
        Logger.write().debug('Found mailbox type "%s"' % mailbox_type)
        for case in Switch(mailbox_type):
            if case('maildir'):
                mail_box = mailbox.Maildir(location)
                break
            if case('mbox'):
                mail_box = mailbox.mbox(location)
                break
            if case('mh'):
                mail_box = mailbox.MH(location)
                break
            if case('babyl'):
                mail_box = mailbox.Babyl(location)
                break
            if case('mmdf'):
                mail_box = mailbox.MMDF(location)
                break
            if case():
                Logger.write().error(
                    'Unknown mailbox type "%s" was specified' % mailbox_type)
                break
    else:
        Logger.write().error('The mailbox path given (%s) does not exist!' %
                             location)
    return mail_box
示例#2
0
def mail_container(value):
    """
    Check that the value points to a valid mail container,
    in URI-style, e.g.: `mbox:///home/username/mail/mail.box`.
    The value is cast to a :class:`mailbox.Mailbox` object.
    """
    if not re.match(r'.*://.*', value):
        raise VdtTypeError(value)
    mburl = urlparse(value)
    if mburl.scheme == 'mbox':
        box = mailbox.mbox(mburl.path)
    elif mburl.scheme == 'maildir':
        box = mailbox.Maildir(mburl.path)
    elif mburl.scheme == 'mh':
        box = mailbox.MH(mburl.path)
    elif mburl.scheme == 'babyl':
        box = mailbox.Babyl(mburl.path)
    elif mburl.scheme == 'mmdf':
        box = mailbox.MMDF(mburl.path)
    else:
        raise VdtTypeError(value)
    return box
# Python Email Communication System
# How to use the email package to read, write, and send
# To copy all mail from a Babyl mailbox to an MH mailbox, converting
# all of the format-specific information that can be converted:

import mailbox

destination = mailbox.MH('~/Mail')
destination.lock()

for message in mailbox.Babyl('~/RMAIL'):
    destination.add(mailbox.MHMessage(message))

destination.flush()
destination.unlock()