All storages, except Pop3, support folders, also called mailboxes. The interface
implemented by all storages supporting folders is called
Zend_Mail_Storage_Folder_Interface. Also all of these classes
have an additional optional parameter called folder, which is the
folder selected after login, in the constructor.
For the local storages you need to use separate classes called
Zend_Mail_Storage_Folder_Mbox or
Zend_Mail_Storage_Folder_Maildir. Both need one parameter called
dirname with the name of the base dir. The format for maildir is as
defined in maildir++ (with a dot as default delimiter), Mbox is a directory hierarchy
with Mbox files. If you don't have a Mbox file called INBOX in your Mbox base dir you
need to set another folder in the constructor.
Zend_Mail_Storage_Imap already supports folders by default.
Examples for opening these storages:
<?php
// mbox with folders
$mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' =>
'/home/test/mail/'));
// mbox with a default folder not called INBOX, also works
// with Zend_Mail_Storage_Folder_Maildir and Zend_Mail_Storage_Imap
$mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' =>
'/home/test/mail/',
'folder' =>
'Archive'));
// maildir with folders
$mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' =>
'/home/test/mail/'));
// maildir with colon as delimiter, as suggested in Maildir++
$mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' =>
'/home/test/mail/',
'delim' => ':'));
// imap is the same with and without folders
$mail = new Zend_Mail_Storage_Imap(array('host' => 'example.com',
'user' => 'test',
'password' => 'test'));
With the method getFolders($root = null) you can get the folder hierarchy starting with
the root folder or the given folder. It's returned as an instance of
Zend_Mail_Storage_Folder, which implements
RecursiveIterator and all children are also instances of
Zend_Mail_Storage_Folder. Each of these instances has a local and
a global name returned by the methods getLocalName() and
getGlobalName(). The global name is the absolute name from the
root folder (including delimiters), the local name is the name in the parent folder.
Table 117. Mail Folder Names
| Global Name | Local Name |
|---|---|
| /INBOX | INBOX |
| /Archive/2005 | 2005 |
| List.ZF.General | General |
If you use the iterator, the key of the current element is the local name. The global
name is also returned by the magic method __toString(). Some
folders may not be selectable, which means they can't store messages and selecting them
results in an error. This can be checked with the method
isSelectable(). So it's very easy to output the whole tree in a
view:
<?php
$folders = new RecursiveIteratorIterator($this->mail->getFolders(),
RecursiveIteratorIterator::SELF_FIRST);
echo '<select name="folder">';
foreach ($folders as $localName => $folder) {
$localName = str_pad('', $folders->getDepth(), '-', STR_PAD_LEFT) .
$localName;
echo '<option';
if (!$folder->isSelectable()) {
echo ' disabled="disabled"';
}
echo ' value="' . htmlspecialchars($folder) . '">'
. htmlspecialchars($localName) . '</option>';
}
echo '</select>';
The current selected folder is returned by the method
getCurrentFolder(). Changing the folder is done with the
method selectFolder(), which needs the global name as
parameter. If you want to avoid to write delimiters you can also use the properties of a
Zend_Mail_Storage_Folder instance:
<?php
// depending on your mail storage and its settings $rootFolder->Archive->2005
// is the same as:
// /Archive/2005
// Archive:2005
// INBOX.Archive.2005
// ...
$folder = $mail->getFolders()->Archive->2005;
echo 'Last folder was '
. $mail->getCurrentFolder()
. "new folder is $folder\n";
$mail->selectFolder($folder);




