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;
   $mgr->registerType(mbox => 'Mail::Box::Mbox');

   # Create folder objects.
   my $folder = $mgr->open(folder => $ENV{MAIL});
   $mgr->close($folder);
   $mgr->copyMessage('Draft', $message);
   $mgr->moveMessage('Outbox', $message1, $message2, create => 1 );

   # Create thread-detectors (see Mail::Box::Threads)
   my $threads = $mgr->threads(folder => $folder);

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 enclosed in the package for more details. You may also want to have a look in the example scripts which come with the module.

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 get 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 manager-class is the only one you create in your program: all other classes will come when needed.

Overview:

  Mail::Box::Manager 
        |
        | open()
        |                           
        v           contains
  Mail::Box::Mbox <-----------> Mail::Box
  (Mail::Box::MH)                ::Mbox::Message
       isa                             isa
     Mail::Box    ............. Mail::Box::Message
                                       isa
                                   MIME::Entity
                                       isa
                                  Mail::Internet

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).

The threads manager

Most messages are replies on other messages. It is pleasant to read mail when you see directly how messages relate. Certainly when the amount of messages grow to dozins a day.

The main manager also keeps overview on the created thread-detection objects, and informs them when the content of a folder is changed. In extention to other Mail-Agents, Mail::Box can show you threads which are spread over more than one folder.

METHODS for the manager

new ARGS

(class method) Create a new 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";

METHODS to handle folders

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.

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). The folder's messages will be withdrawn from the known message-threads.

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

Examples:

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

closeAllFolders calls close for each folder managed by this object.

appendMessages [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->appendMessages('=send', $message, folderdir => '/');
   $mgr->appendMessages('=received', $inbox->messages);
   $mgr->appendMessages($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.

METHODS to handle threaders

threads OPTIONS

Create a new object which keeps track on message threads. You can read about the possible options in the Mail::Box::Threads manpage.

Example:

    $mgr->threads(folders => [ $inbox, $send ]);
toBeThreaded FOLDER, MESSAGES
toBeUnthreaded 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 (respectively going out).

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.322