The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mail::Box::Manager - manage a set of folders

INHERITANCE

 Mail::Box::Manager
   is a Mail::Reporter

SYNOPSIS

 use Mail::Box::Manager;
 my $mgr     = new Mail::Box::Manager;

 # Create folder objects.
 my $folder   = $mgr->open(folder => $ENV{MAIL});
 my $message1 = $folder->message(0);
 $mgr->copyMessage('Draft', $message);

 my @messages = $folder->message(0,3);
 $mgr->moveMessage('Outbox', @messages, create => 1 );
 $mgr->close($folder);

 # Create thread-detectors (see L<Mail::Box::Thread::Manager|Mail::Box::Thread::Manager>)
 my $t       = $mgr->threads($inbox, $outbox);

 my $threads = $mgr->threads(folder => $folder);
 foreach my $thread ($threads->all)
 {   $thread->print;
 }

 $mgr->registerType(mbox => 'Mail::Box::MyType');

DESCRIPTION

The manager keeps track on a set of open folders and a set of message-thread supporting objects. You are not obliged to use this object (you can directly create a Mail::Box::Mbox if you prefer), but you will create more portable and safer code if you do use it.

METHODS

Constructors

Mail::Box::Manager->new(ARGS)

     Option               Defined in       Default                   
     default_folder_type                   C<'mbox'>                 
     folder_types                          <all standard types>      
     folderdir                             C<[ '.' ]>                
     folderdirs                            <synonym for C<folderdir>>
     log                  L<Mail::Reporter>  C<'WARNINGS'>             
     trace                L<Mail::Reporter>  C<'WARNINGS'>             

    . default_folder_type NAME|CLASS

      Specifies the default folder type for newly created folders. If this option is not specified, the most recently registered type is used (see registerType() and the new(folder_types) option.

    . folder_types NEW-TYPE | ARRAY-OF-NEW-TYPES

      Add one or more new folder types to the list of known types. The order is important: when you open a file without specifying its type, the manager will start trying the last added list of types, in order.

      Each TYPE is specified as an array which contains name, class, and defaults for options which overrule the usual defaults. You may specify folder-specific defaults as OPTIONS. They override the settings of the manager.

    . folderdir DIRECTORY

      The default directory, or directories, where folders are located. Mail::Box::Manager can autodetect the existing folder-types. There may be different kinds of folders opened at the same time, and messages can be moved between those types, although that may result in a loss of information depending on the folder types.

    . folderdirs [DIRECTORIES]

    . log LEVEL

    . trace LEVEL

Manage folders

$obj->close(FOLDER, OPTIONS)

    close removes the specified folder from the list of open folders. Indirectly it will update the files on disk if needed (depends on the Mail::Box::new(save_on_exit) flag for each folder). OPTIONS are passed to Mail::Box::close() of the folder.

    The folder's messages will also be withdrawn from the known message threads. You may also close the folder directly. The manager will be informed about this event and take appropriate actions.

    Example:

     my $inbox = $mgr->open('inbox');
     $mgr->close($inbox);
     $inbox->close;        # alternative

$obj->closeAllFolders(, OPTIONS)

    closeAllFolders calls close() for each folder managed by this object. It is called just before the program stops (before global cleanup).

$obj->delete(FOLDERNAME [,OPTIONS])

    Remove the named folder, including all its sub-folders. The OPTIONS are the same as those for open().

    The deletion of a folder can take some time. Dependent on the type of folder, the folder must be read first. For some folder-types this will be fast.

$obj->folderTypes

    Returns the list of currently defined folder types.

    Example:

     print join("\n", $manager->folderTypes), "\n";

$obj->isOpenFolder(FOLDER)

    Returns true if the FOLDER is currently open.

    Example:

     print "Yes\n" if $mgr->isOpenFolder('Inbox');

$obj->open([FOLDERNAME], OPTIONS)

    Open a folder which name is specified as first parameter or with the option flag folder. The folder type is autodetected unless the type is specified.

    open carries options for the manager which are described here, but may also have additional options for the folder type. For a description of the folder options, see the options to the constructor Mail::Box::new() for each type of mail box.

     Option     Defined in       Default                 
     create                      <false>                 
     folder                      C<$ENV{MAIL}>           
     folderdir                   C<'.'>                  
     type                        <first, usually C<mbox>>

    . create BOOLEAN

      Create the folder if it does not exist. By default, this is not done. The type option specifies which type of folder is created.

    . folder NAME|URL

      Which folder to open, specified by NAME or special URL. The URL format is composed as

       type://username:password@hostname:port/foldername

      Like real URLs, all fields are optional and have smart defaults, as long as the string starts with a known folder type. Far from all folder types support all these options, but at least they are always split-out. Be warned that special characters in the password should be properly url-encoded.

      When you specify anything which does not match the URL format, it is passed directly to the new method of the folder which is opened.

    . folderdir DIRECTORY

      The directory where the folders are usually stored.

    . type FOLDERTYPENAME|FOLDERTYPE

      Specify the type of the folder. If you do not specify this option while opening a folder for reading, the manager checks all registered folder types in order for the ability to open the folder. If you open a new folder for writing, then the default will be the most recently registered type. (If you add more than one type at once, the first of the list is used.)

    Example: opening folders via the manager

     my $jack  = $manager->open(folder => '=jack',
        type => 'mbox');
    
     my $rcvd  = $manager->open('myMail',
        type => 'Mail::Box::Mbox', access => 'rw');
    
     my $inbox = $manager->open('Inbox')
        or die "Cannot open Inbox.\n";
    
     my $pop   = 'pop3://myself:secret@pop3.server.com:120/x';
     my $send  = $manager->open($url);
    
     my $send  = $manager->open(folder => '/x',
       type => 'pop3', username => 'myself', password => 'secret'
       server_name => 'pop3.server.com', server_port => '120');

$obj->openFolders

    Returns a list of all open folders.

$obj->registerType(TYPE, CLASS [,OPTIONS])

    With registerType you can register one TYPE of folders. The CLASS is compiled automatically, so you do not need to use them in your own modules. The TYPE is just an arbitrary name.

    The added types are prepended to the list of known types, so they are checked first when a folder is opened in autodetect mode.

    Example:

     $manager->registerType(mbox => 'Mail::Box::Mbox',
         save_on_exit => 0, folderdir => '/tmp');

Move messages to folders

$obj->appendMessage([FOLDER|FOLDERNAME,] MESSAGES, OPTIONS)

    Append one or more messages to a folder (therefore, an appendMessages() is defined as well). You may specify a FOLDERNAME or an opened folder as the first argument. When the name is that of an open folder, it is treated as if the folder-object was specified, and not directly access the folder-files. You may also specify the foldername as part of the options list.

    If a message is added to an already opened folder, it is only added to the structure internally in the program. The data will not be written to disk until a write of that folder takes place. When the name of an unopened folder is given, the folder is opened, the messages stored on disk, and then the folder is closed.

    A message must be an instance of a Mail::Message. The actual message type does not have to match the folder type--the folder will try to resolve the differences with minimal loss of information. The coerced messages (how the were actually written) are returned as list.

    The OPTIONS is a list of key/values, which are added to (overriding) the default options for the detected folder type.

    Example:

     $mgr->appendMessage('=send', $message, folderdir => '/');
     $mgr->appendMessage($received, $inbox->messages);
    
     my @appended = $mgr->appendMessages($inbox->messages,
        folder => 'Drafts');
     $_->label(seen => 1) foreach @appended;

$obj->copyMessage([FOLDER|FOLDERNAME,] MESSAGES, OPTIONS)

    Copy a message from one folder into another folder. If the destination folder is already opened, the copied message is stored in memory and written to disk when a write of the folder is later performed. Otherwise, the destination folder will be opened, the message written, and then the folder closed.

    You need to specify a folder's name or folder object as the first argument, or in the options list. The options are the same as those which can be specified when opening a folder.

    Example:

     my $drafts = $mgr->open(folder => 'Drafts');
     my $outbox = $mgr->open(folder => 'Outbox');
     $mgr->copyMessage($outbox, $drafts->message(0));
    
     my @messages = $drafts->message(1,2);
     $mgr->copyMessage('=Trash', @messages.
        folderdir => '/tmp', create => 1);
    
     $mgr->copyMessage($drafts->message(1),
        folder => '=Drafts' folderdir => '/tmp',
        create => 1);

$obj->moveMessage([FOLDER|FOLDERNAME,] MESSAGES, OPTIONS)

    Move a message from one folder to another. Be warned that removals from a folder only take place when the folder is closed, so the message is only flagged to be deleted in the opened source folder.

     $mgr->moveMessage($received, $inbox->message(1))

    is equivalent to

     $mgr->copyMessage($received, $inbox->message(1));
     $inbox->message(1)->delete;

Manage message threads

$obj->threads([FOLDERS], OPTIONS)

    Create a new object which keeps track of message threads. You can read about the possible options in Mail::Box::Thread::Manager. As OPTIONS specify one folder or an array of FOLDERS. It is also permitted to specify folders before the options.

    Example:

     my $t1 = $mgr->threads(folders => [ $inbox, $send ]);
     my $t2 = $mgr->threads($inbox);
     my $t3 = $mgr->threads($inbox, $send);

Internals

$obj->decodeFolderURL(URL)

    Try to decompose a folder name which is specified as URL (see open()) into separate options.

$obj->toBeThreaded(FOLDER, MESSAGES)

    Signal to the manager that all thread managers which are using the specified folder must be informed that new messages are coming in.

$obj->toBeUnthreaded(FOLDER, MESSAGES)

    Signal to the manager that all thread managers which are using the specified folder must be informed that new messages are or going out.

Error handling

$obj->AUTOLOAD

$obj->defaultTrace([LEVEL, [LEVEL])

Mail::Box::Manager->defaultTrace([LEVEL, [LEVEL])

$obj->errors

$obj->log([LEVEL [,STRINGS]])

Mail::Box::Manager->log([LEVEL [,STRINGS]])

$obj->logPriority(LEVEL)

Mail::Box::Manager->logPriority(LEVEL)

$obj->logSettings

$obj->notImplemented

$obj->report([LEVEL])

$obj->reportAll([LEVEL])

$obj->trace([LEVEL])

$obj->warnings

Cleanup

$obj->DESTROY

$obj->inGlobalDestruction

DIAGNOSTICS

Error: Folder $name is not a Mail::Box; cannot add a message.

The folder where the message should be appended to is an object which is not a folder type which extends Mail::Box. Probably, it is not a folder at all.

Warning: Folder does not exist, failed opening $type folder $name.

The folder does not exist and creating is not permitted (see open(create)) or did not succeed. When you do not have sufficient access rights to the folder (for instance wrong password for POP3), this warning will be produced as well.

The manager tried to open a folder of the specified type. It may help to explicitly state the type of your folder with the type option. There will probable be another warning or error message which is related to this report, and provides more details.

Warning: Folder type $type is unknown, using autodetect.

The specified folder type (see open(type), possibly derived from the folder name when specified as url) is not known to the manager. This may mean that you forgot to require the Mail::Box extension which implements this folder type, but probably it is a typo. Usually, the manager is able to figure-out which type to use by itself.

Error: Illegal folder URL '$url'.

The folder name was specified as URL, but not according to the syntax. See decodeFolderURL() for an description of the syntax.

Error: No foldername specified to open.

open() needs a folder name as first argument (before the list of options), or with the folder option within the list. If no name was found, the MAIL environment variable is checked. When even that does not result in a usable folder, then this error is produced. The error may be caused by an accidental odd-length option list.

Error: Package $package does not implement $method.

Fatal error: the specific package (or one of its superclasses) does not implement this method where it should. This message means that some other related classes do implement this method however the class at hand does not. Probably you should investigate this and probably inform the author of the package.

Error: Use appendMessage() to add messages which are not in a folder.

You do not need to copy this message into the folder, because you do not share the message between folders.

Warning: Use moveMessage() or copyMessage() to move between open folders.

The message is already part of a folder, and now it should be appended to a different folder. You need to decide between copy or move, which both will clone the message (not the body, because they are immutable).

Error: Will never create a folder $name without having write access.

You have set open(create), but only want to read the folder. Create is only useful for folders which have write or append access modes (see Mail::Box::new(access)).

REFERENCES

See the Mail::Box website at http://perl.overmeer.net/mailbox/ for more details.

COPYRIGHTS

Module version 2.042. Written by Mark Overmeer (mark@overmeer.net). See the ChangeLog for other contributors.

Copyright (c) 2001-2003 by the author(s). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.