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
- is a
Mail::Box::Manageris extended byMail::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β--Defaultautodetectβundefdefault_folder_typeβ'mbox'folder_typesβ<all standard types>folderdirβ[ '.' ]folderdirsβ<synonym for C<folderdir>>
autodetect => $type|\@typesβ Select only a subset of the folder@typeswhich 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$typefor newly created folders. If this option is not specified, the most recently registered type is used (seeregisterType()and thenew(folder_types)option.folder_types => $typedef|\@typedefsβ Add one or more new folder@typedefsto 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. TheMail::Box::Managercan 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)β WithregisterTypeyou can register one$typeof folders. The$classis compiled automatically, so you do not need tousethem in your own modules. The$typeis 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@paramsare passed, thendiscoverThreads()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$folderfrom the index of open folders. Indirectly it will update the files on disk if needed (depends on theMail::Box::new(save_on_exit)flag for each folder).%optionsare passed toMail::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β--Defaultclose_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)βcloseAllFolderscallsclose()for each folder managed by this object. It is called just before the program stops (before global cleanup).$obj->isOpenFolder($folder)β Returnstrueif the$folderis 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 flagfolder. The folder type is autodetected unless thetypeis 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β--Defaultauthenticateβ'AUTO'createβfalsefolderβ$ENV{MAIL}folderdirβ'.'typeβ<first, usually 'mbox'>
authenticate => $type|\@types|'AUTO'β The$typeof authentication to be used, or an ARRAY of@typeswhich 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. Thetypeoption specifies which type of folder is created.folder => $name|$urlβ Which folder to open, specified by$nameor 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%optionsare the same as those foropen().
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β--Defaultrecursiveβ<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, anappendMessages()is defined as well). You may specify a$foldernameor 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@foldersas 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
- ubuntu stonking: libmail-box-perl 4.02-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms