The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

SYNOPSIS

   use Mail::Box::Manager;
   my $mgr    = new Mail::Box::Manager;
   my $folder = $mgr->open(folder => $ENV{MAIL});
   $mgr->registerType(mbox => 'Mail::Box::Mbox');
   $mgr->close($folder);
   $mgr->copyMessage('Draft', $message);
   $mgr->moveMessage('Outbox', $message1, $message2, create => 1 );

DESCRIPTION

This code is beta, which means that there are no serious applications written with it yet. Please inform the author when you have, so this module can go to stable. Read the STATUS file inclosed in the package for more details.

The Mail::Box package can be used as back-end to Mail User-Agents (MUA's), and has special features to help those agents to have fast access to folder-data. These features may delay access to folders for other kinds of applications. Maybe Mail::Procmail has more for you in such cases.

The folder manager

The folder manager maintains a set of folders (mail-boxes). Those folders may be of different types. Most folder-types can be detected automatically. This class is the only one you create in your program: all other classes will come when needed.

Overview:

  Mail::Box::Manager
        |
        | open()
        |              message()
        v             ,--------->  Mail::Box::Message
     Mail::Box      /                    isa
  (Mail::Box::Mbox)                  MIME::Entity
   (Mail::Box::MH)                   : :
    : : :                            : :
    : : :                            : Mail::Box::Message::Dummy
    : : :                            Mail::Box::Message::NotParsed
    : : Mail::Box::Tie
    : Mail::Box::Threads
    Mail::Box::Locker

All classes are written to be extendible. The most complicated work is done in MIME::Entity, which is written and maintained by Eryq (eryq@zeegee.com).

METHODS

new ARGS

(class method) Create a new folder folder-manager. This constructor may carry the following options:

  • folder_types => [ NAME => CLASS [,OPTIONS] ]

  • folder_types => [ [ NAME => CLASS [,OPTIONS] ], [...] ]

    Add one or more 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 set of types, with precedence for the first of that list.

    You may specify folder-specific defaults as OPTIONS. They overrule the settings of the manager.

  • default_folder_type => NAME|CLASS

    When a new folder is created, it is of this type. If this option is not specified, the most recently registered type is used (see registerType and the folder_types-option.

  • folderdir => DIRECTORY

  • folderdirs => [ DIRECTORY, ... ]

    The default directory, respectively 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.

registerType TYPE => CLASS [,OPTIONS]

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

The added types are put in front of the known types, so are checked first when a folder is opened in autodetect mode.

Example:

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

The folderTypes returns the list of currently defined types.

Example:

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

Open a folder. The folder-type is autodetected unless the type is specified. open carries options for the manager, which are described here, but may have additional options for each type of folders. See the options to the constructor (the new method) for each type of mail-box, but first the new of Mail::Box for the general options.

The options which are most common to open():

  • folder => FOLDERNAME

    Which folder to open. The default folder is $ENV{MAIL}.

  • folderdir => DIRECTORY

    The directory where the folders are usually stored.

  • type => FOLDERTYPENAME|FOLDERTYPE

    Specify that the folder is of a specific type. When you do not specify this and you open the folder for ready, it checks all registered folder-types for the ability to open the folder. If you open a new folder to write, 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 taken).

    Examples:

       $manager->open(folder => '=jack', type => 'mbox');
       $manager->open(type => 'Mail::Box::Mbox');
  • create => BOOL

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

addOpenFolder FOLDER
openFolders

As could be expected from the name, addOpenFolder adds a new folder to set of open folders. Ignores undefined value for FOLDER. openFolders returns a list of all open folders.

isOpenFolder FOLDER

Returns folder when the FOLDER is kept open.

Example:

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

close removes the specified folder from the list of open folders. Indirectly it will update the files on disk if needed (depends on the save_on_exit flag to each seperate folder).

You may also close the folder directly. The manager will be informed about this event.

Examples:

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

closeAllFolders calls close for each folder managed by this object.

appendMessage [FOLDER|FOLDERNAME,] MESSAGES, OPTIONS

Append one or more messages to a folder. As first argument, you may specify a FOLDERNAME or an opened folder. When the name is that of an opened folder, is it treated as if the folder-structure was specified. You may also specify the foldername as part of the option-list.

When a message is added to an 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, data message data is immediately stored on disk.

A message must be an instance of an MIME::Entity. The actual type may be in conflict with the requirements for the folder-type where the data is added. However, this is not a concern of the caller: the folders will try to resolve the differences with minimal loss of information.

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

Examples:

   $mgr->appendMessage('=send', $message, folderdir => '/');
   $mgr->appendMessage('=received', $inbox->messages);
   $mgr->appendMessage($inbox->messages, folder => 'Drafts');
copyMessage [FOLDER|FOLDERNAME,] MESSAGES, OPTIONS

Copy a message from one folder into an other folder. If that other folder has not been opened, the copy behaves like an appendMessage(). Otherwise, the data from the message is copied and added to the other open folder.

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

Examples:

    my $drafts = $mgr->open(folder => 'Drafts');
    my $outbox = $mgr->open(folder => 'Outbox');
    $mgr->copyMessage($outbox, $drafts->message(0));

    $mgr->copyMessage('Trash', $drafts->message(1), $drafts->message(2),
               folderdir => '/tmp', create => 1);

    $mgr->copyMessage($drafts->message(1), folder => 'Drafts'
               folderdir => '/tmp', create => 1);
moveMessage [FOLDER|FOLDERNAME,] MESSAGES, OPTIONS

Move a message from one folder to the next. 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;
delete FOLDERNAME [,OPTIONS]

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

To accomplish a full removal, all folders have to be opened first, while Mail::Box messages may have parts of them stored in external files, which must be removed too.

AUTHOR

Mark Overmeer (Mark@Overmeer.net). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

VERSION

This code is beta, version 1.111