Mail::...anager(3pm)

πŸ’– Mail::Box::Manager β€” manage a set of folders πŸ’–

okay so bestie, this is the Mail::Box::Manager page from User Contributed Perl Documentation. She manages a whole set of folders like the queen she is πŸ‘‘. Let's get into it, no cap πŸ’…

🧬 INHERITANCE 🧬

the family tree, periodt

  • Mail::Box::Manager
    • is a Mail::Reporter
  • Mail::Box::Manager is extended by
    • Mail::Box::Manage::User

✨ SYNOPSIS ✨

vibe check: here's the whole flex in code form πŸ¦‹

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 Mail::Box::Thread::Manager)
my $t       = $mgr->discoverThreads($inbox, $outbox);

my $threads = $mgr->discoverThreads(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.

Extends "DESCRIPTION" in Mail::Reporter.

πŸ“š METHODS πŸ“š

Extends "METHODS" in Mail::Reporter.

πŸ—οΈ Constructors πŸ—οΈ

Extends "Constructors" in Mail::Reporter.

$class->new(%options)

Inherited, see "Constructors" in Mail::Reporter

here are the options and their defaults, bestie:

  • -Option β€” --Default
    • autodetect β€” undef
    • default_folder_type β€” 'mbox'
    • folder_types β€” <all standard types>
    • folderdir β€” [ '.' ]
    • folderdirs β€” <synonym for C<folderdir>>
  • autodetect => $type|\@types β€” Select only a subset of the folder @types which are implemented by MailBox to be detected automatically. This may improve the auto-detection of folder types. Normally, all folder types will be tried when a folder's name is incorrect, but this option limits the types which are checked and therefore may respond faster. speed queen behavior ⚑
  • default_folder_type => $type|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 => $typedef|\@typedefs β€” Add one or more new folder @typedefs 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 $typedef 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. The 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]

πŸ“ Attributes πŸ“

Extends "Attributes" in Mail::Reporter.

  • $obj->defaultFolderType() β€” Returns the default folder type, some class name.
  • $obj->folderTypeDefs() β€” (no description given, just vibes)
  • $obj->folderTypes() β€” Returns the list of currently defined folder types.

Β» example:

print join("\n", $manager->folderTypes), "\n";
  • $obj->folderdir() β€” In list context, this returns all folderdirs specified. In SCALAR context only the first.
  • $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');
  • $obj->threads( [@params] ) β€” return the descovered threads (cached value). When any @params are passed, then discoverThreads() will be called for backwards compatibility.

πŸ—‚οΈ Manage open folders πŸ—‚οΈ

the part where she actually runs the folder household 🧹

  • $obj->close($folder, %options) β€” Remove the specified $folder from the index 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.

  • -Option β€” --Default
    • close_by_self β€” <C<false>>
  • close_by_self => BOOLEAN β€” Used internally to avoid confusion about how the close was started. Do not change this.

Β» 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->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 β€” --Default
    • authenticate β€” 'AUTO'
    • create β€” false
    • folder β€” $ENV{MAIL}
    • folderdir β€” '.'
    • type β€” <first, usually 'mbox'>
  • authenticate => $type|\@types|'AUTO' β€” The $type of authentication to be used, or an ARRAY of @types which the client prefers. The server may provide preferences as well, and that order will be kept. This option is only supported by a small subset of folder types, especially by POP and IMAP.
  • 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.)

Currently, the types are "mbox", "mh", "maildir", "pop3", "pop3s", "imap4", and "imap4s". You may also use names "pop", "pops", "imap", and "imaps".

Β» 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.

πŸ—‘οΈ Manage existing folders πŸ—‘οΈ

  • $obj->delete($foldername, %options) β€” Remove the named folder. 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.

  • -Option β€” --Default
    • recursive β€” <folder's default>
  • recursive => BOOLEAN β€” Some folder can only be recursively deleted, other have more flexibility.

πŸ“¬ 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) for @appended;

πŸ‘― $obj->copyMessage( [$folder|$foldername], @messages, %options )

okay bestie so this one takes @messages and copies them out of one folder and into another one. if the destination folder is already open? it uses Mail::Box::copyTo() πŸ’¨. if it's not open, it calls Mail::Box::appendMessages() instead. chef's choice, you don't pick.

you have to hand over either a folder's name or an actual folder object as the first argument, or sneak it into the options list. and the options here? they're exactly the same ones you'd pass when opening a folder. that's it, that's the tea β˜•

-Option--Default
 share   false
  • share => BOOLEAN β€” try to share the physical storage of the messages 🫢. the folder types might be totally different from each other, and whether this works at all depends on the actual folder where the message lands. if sharing just isn't possible, it's silently ignored. no drama, no error, just vibes ✨

Β» 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), create => 1,
   folder => '=Drafts' folderdir => '/tmp');

🚚 $obj->moveMessage( [$folder|$foldername], @messages, %options )

moves those @messages from one $folder over to another one. main character relocation arc πŸ’…

⚠️ BE WARNED β€” removals from a folder only actually happen when the folder gets closed!! so the message in the opened source folder is merely flagged to be deleted for now. not gone. just marked 🫣
⚠️ BE WARNED β€” message labels can get straight-up lost when you move a message from one folder type to another 😭. there is an attempt to translate the labels, but apps interpret labels so differently that some things just don't survive the journey.
$mgr->moveMessage($received, $inbox->message(1))

is equivalent to

$mgr->copyMessage($received, $inbox->message(1), share => 1);
$inbox->message(1)->delete;
-Option--Default
 share   true
  • share => BOOLEAN

🧡 Manage message threads

🌳 $obj->discoverThreads( [@folders], %options )

makes you a brand new Mail::Box::Thread::Manager object, which collects up all the message threads it finds across the combined @folders you specified. like a group chat reconstruction, but make it ~archival~ 🌸

every other %options gets passed straight down to Mail::Box::Thread::Manager::new().

-Option       --Default
 folders        <positional folder arguments>
 threader_type  Mail::Box::Thread::Manager
  • folders => \@folders β€” if you don't wanna pass the @folders as positional params, you can slide them in through this named parameter instead. you've got options, literally πŸŽ€
  • threader_type => CLASS

Β» example:

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

πŸ› οΈ Internals

πŸ” $obj->decodeFolderURL($url)

tries to break down a folder name that's given as $url (peep open()) into its separate options. special characters like the @-sign, colon, and slash used inside the user or password parts must be passed $url-encoded, no cap πŸ”

πŸ“₯ $obj->toBeThreaded($folder, $messages)

signals to the manager that every thread manager using the specified folder has to be told new messages are coming in πŸ“¬

πŸ“€ $obj->toBeUnthreaded($folder, $messages)

signals to the manager that every thread manager using the specified folder has to be told that messages are β€” or are going β€” out πŸšͺ

🚨 Error handling

extends "Error handling" in Mail::Reporter.

πŸŒ€ $obj->AUTOLOAD()

inherited, see "Error handling" in Mail::Reporter

πŸŒ€ $obj->notImplemented()

inherited, see "Error handling" in Mail::Reporter

🧹 Cleanup

extends "Cleanup" in Mail::Reporter.

πŸŒ€ $obj->DESTROY()

inherited, see "Cleanup" in Mail::Reporter


πŸ’« DETAILS

in loads of places in the docs you'll read that it's useful to have a manager object. and there are two of them: Mail::Box::Manager, which keeps a whole set of open folders in check, and an extension of it, Mail::Box::Manage::User πŸ‘‘

πŸ“‚ Managing open folders

it's a good move to start your program by creating a folder manager object, a Mail::Box::Manager. it lifts a few burdens right off your neck 🫢

  • autodetect the type of folder which is used.

meaning your application can be fully folder type independent ✨

  • autoload the required modules

so many modules are involved in MailBox that some lazy autoloading of code is genuinely useful. the manager knows which modules belong to which type of folder 🧠

  • avoid double openings

your own programming mistakes might make the same folder open twice, and the result of that could be very destructive πŸ’€. so the manager keeps track of all open folders and stops the same folder from being opened a second time.

  • close folders at clean-up

when the program ends, the manager cleanly closes every folder still open. this is required, because Perl's autodestruct sequence works in an unpredictable order πŸ˜΅β€πŸ’«

  • message thread detection

MailBox can discover message threads that span multiple folders 🌈. any set of open folders can be grouped into a tree of replies on replies on replies. when a folder closes it's automatically removed from the threads, and a new folder can dynamically be added to the structure.

the manager really does simplify things, so it honestly should be the base of all programs. that said, you can still write useful programs without it πŸ’œ

πŸ‘€ Managing a user

one step further along is the Mail::Box::Manage::User object (since MailBox v2.057), which doesn't only keep track of open folders, it also collects info about folders that aren't open.

the user class is, as the name says, targeted at managing one single user πŸŽ€. where Mail::Box::Manager will open any set of folder files β€” probably from multiple users β€” the user class wants one root folder directory.

in a lot of aspects, the user manager simplifies the job for user-based servers and other user-centric applications by setting smart defaults ✨


🩺 DIAGNOSTICS

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

the folder the message is supposed to be appended to is an object that isn't a folder type extending Mail::Box. probably it's not a folder at all 🫠 cast by appendMessage()

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

you don't need to copy this message into the folder, because you're not sharing the message between folders. cast by copyMessage() πŸ’…

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

the message is already part of a folder, and now it's supposed to be appended to a different folder. you need to pick between copy or move β€” and both will clone the message (not the body, because those are immutable). cast by appendMessage() πŸ”€

  • Error: class $package does not implement method $method.

fatal error: the specific $package (or one of its superclasses) does not implement this method where it should. this message means some other related classes do implement this method, but the class at hand does not 🀨. you should probably investigate, and probably inform the author of the package. cast by notImplemented()

  • Error: failed for folder default $class: $@

cast by open() πŸ’₯

  • Error: folder $name does not exist, failed opening $type.

the folder doesn't exist and creating it isn't permitted (see open(create)) or didn't succeed. when you don't have sufficient access rights to the folder (wrong password for POP3, for instance), this warning gets produced too πŸ™ˆ

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 probably be another warning or error message related to this report with way more detail on the cause. you could also take a peek at new(autodetect) and new(folder_types) πŸ‘€ cast by open()

  • Error: folder $name is already open.

you can't ask the manager for a folder that's already open. in some older releases (before MailBox 2.049) this was permitted, but then behaviour changed, because so many nasty side-effects were to be expected 😬. for instance, a Mail::Box::update() on one folder handle would influence the second one, probably unexpectedly. cast by open()

  • Warning: folder type $type is unknown, using autodetect.

the folder type you specified (see open(type), possibly derived from the folder name when it was specified as url) isn't known to the manager. this could mean you forgot to require the Mail::Box extension implementing that folder type, but it's probably a typo πŸ“. usually the manager can figure out which type to use all by itself. cast by open()

  • Error: illegal folder URL '$url'.

the folder name was specified as a URL, but not according to the syntax 🚫. see decodeFolderURL() for a description of the syntax. cast by open()

  • Error: no foldername specified to open.

open() needs a folder name as its first argument (before the list of options), or with the "folder" option inside the list. if no name was found, the MAIL environment variable gets checked 🌱. when even that doesn't result in a usable folder, this error is produced. it may be caused by an accidental odd-length option list. cast by open()

  • Error: threader $class is not derived from $base.

cast by discoverThreads() 🧡

  • Error: unusable threader $class: $@

cast by discoverThreads() 🧡

  • Warning: will never create a folder $name without having write access.

you set open(create), but you only want to read the folder πŸ™ƒ. create is only useful for folders with write or append access modes (see Mail::Box::new(access)). cast by open()

  • Error: you need to pass a $base derived threader, got $class.

cast by discoverThreads() 🧡


πŸ‘€ SEE ALSO

this module is part of Mail-Box version 4.02, built on April 10, 2026.

website: http://perl.overmeer.net/CPAN/

πŸ“œ LICENSE

for contributors see file ChangeLog.

this software is copyright (c) 2001-2026 by Mark Overmeer.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's a wrap on this era bestie πŸŽ€ the page footer says perl v5.40.1, dated 2026-05-09, filed under Mail::...anager(3pm) β€” so you know exactly which lil snapshot of the docs you just devoured πŸ’– mwah, go open some folders responsibly ✨

πŸ“¦ where this page comes from