Core\Imap\Mailbox::folders
Returns the name of every folder in the mailbox, such as INBOX, Sent and Archive.
Pass a name from this list to folder to open that folder. The mail server writes the names, so each one is a tainted string (text from outside your program). You can print a name and pass it to folder. A tainted string cannot be a plain string, so it does not compile as a file path. The compiler error names the method that makes it safe, such as Core\IO::within for a path.
INBOX is the folder for new mail on every server. The names of the other folders differ from one server to the next, so a script that needs Archive or Junk checks the list first. The names are in the order the server sends them.
Good to know: a directory block is a folder on disk that works like a mailbox, for tests and local work. In such a block, each subdirectory is one folder. The names are in alphabetical order, with capital letters before small letters.
Return value
Returns array<string>. The names, in the order the server sends them. A directory block returns them in alphabetical order. The server writes the names, so they are tainted (text from outside your program).
Errors
IOErrorThe server does not answer.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Imap reads a mailbox over IMAP (the protocol a program uses to read a mailbox on a server). A program opens an [imap.<name>] block of nvs.toml by its name, and then searches, fetches, moves, flags and deletes messages. For tests, a directory block reads a folder of .eml files instead of a server. |
Examples
The examples below run with this configuration file.
# The `inbox` block reads the folder `mailbox` beside this file. It needs no mail server.[imap.inbox]directory = "mailbox"
# The program may open the `inbox` block.[capabilities.imap]connect = ["inbox"]List the folders
<?nvs// Shows how to list the folders of a mailbox.
use Core\Imap;use Core\Str;
var $mailbox = Imap::open('inbox');
// This prints "INBOX, Junk, Sent".echo Str::join($mailbox->folders(), ', '), "\n";INBOX, Junk, SentCount the messages in each folder
<?nvs// Shows how to open each folder of the list and count its messages.
use Core\Arr;use Core\Imap;
var $mailbox = Imap::open('inbox');
// A `tainted string` is text from outside your program.// The server writes the folder names, so each name is a `tainted string`.foreach ($mailbox->folders() as tainted string $name) { var $folder = $mailbox->folder($name); echo $name, ': ', Arr::count($folder->search()), "\n";}INBOX: 2Junk: 1Sent: 1Check for a folder before using it
<?nvs// Shows how a script checks that the folders it needs exist before it moves any message.
use Core\Arr;use Core\Imap;
var $mailbox = Imap::open('inbox');var $names = $mailbox->folders();array<string> $needs = ['Junk', 'Archive'];
// This mailbox has a `Junk` folder, but no `Archive` folder.foreach ($needs as string $needed) { if (Arr::contains($names, $needed)) { echo $needed, ": found\n"; } else { echo $needed, ": missing. Create it in your mail program first.\n"; }}Junk: foundArchive: missing. Create it in your mail program first.