Skip to content

Core\Imap\Mailbox::folder

$mailbox->folder(string $name): Core\Imap\Folder$mailbox->folder(
    string $name,
): Core\Imap\Folder

Opens one folder of the mailbox, such as INBOX, and returns it as a Core\Imap\Folder.

You search, read, move, flag and delete messages through the folder. Pass a name that folders returns. INBOX exists on every server. If the folder does not exist, folder throws an IOError.

You can open several folders of the same mailbox, and use them one after the other. Each message in a folder has a UID (the number the server gives each message in a folder). A UID belongs to one folder, so UID 1 in INBOX and UID 1 in Sent are two different messages.

Good to know: the name is sent to the server as text, so it cannot add a command. A directory block is a folder on disk that works like a mailbox, for tests and local work. In such a block, a name with /, \, : or .. throws a RuntimeError. A name that ends with a dot or a space also throws a RuntimeError.

Parameters

  • $namestringThe folder name, such as INBOX, as folders returns it.

Return value

Returns Core\Imap\Folder. The folder.

Errors

  • RuntimeErrorThe block is a directory block, and the name is not allowed there. Such a block is a folder on disk that works like a mailbox. A name is not allowed when it is empty, . or .., or has a /, \, : or control character. A name that ends with a dot or a space is not allowed either.
  • IOErrorThe folder does not exist, or the server does not answer.

Changelog

VersionChange
0.0.6Core\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.

nvs.toml
# 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"]

Open the inbox

<?nvs
// Shows how to open the inbox and print the subject of each message in it.
use Core\Imap;
var $inbox = Imap::open('inbox')->folder('INBOX');
foreach ($inbox->search() as int $uid) {
echo $uid, ' ', $inbox->fetch($uid)->subject(), "\n";
}
Output
1 Where is my order?
2 Can I change my address?

A folder that does not exist

<?nvs
// Shows the errors `folder` throws for a folder that does not exist and for a name that is not allowed.
use Core\Imap;
var $mailbox = Imap::open('inbox');
// This mailbox has no `Archive` folder, so `folder` throws an `IOError`.
try {
$mailbox->folder('Archive');
} catch (IOError $missing) {
echo "Archive: the folder does not exist\n";
}
// A folder name with `..` could point outside the mailbox, so it throws a `RuntimeError`.
try {
$mailbox->folder('../INBOX');
} catch (RuntimeError $badName) {
echo $badName->message, "\n";
}
Output
Archive: the folder does not exist
Core\Imap\Mailbox::folder: `../INBOX` is not a folder of `[imap.inbox]`. A folder of a directory block is one directory name, without `/`, `\`, `:` or `..`. It does not end with a dot or a space.

Find the reply to a question

<?nvs
// Shows how to use two folders of one mailbox: the questions in `INBOX` and the replies in `Sent`.
use Core\Arr;
use Core\Imap;
var $mailbox = Imap::open('inbox');
var $inbox = $mailbox->folder('INBOX');
var $sent = $mailbox->folder('Sent');
// A `tainted string` is text from outside your program.
// A subject comes from the sender of the mail, so it is a `tainted string`.
array<tainted string> $replied = [];
foreach ($sent->search() as int $uid) {
$replied[] = $sent->fetch($uid)->subject();
}
// UID 1 in `INBOX` and UID 1 in `Sent` are two different messages.
foreach ($inbox->search() as int $uid) {
tainted string $subject = $inbox->fetch($uid)->subject();
bool $done = Arr::contains($replied, 'Re: ' . $subject);
echo $subject, ($done ? ': replied' : ': waiting for a reply'), "\n";
}
Output
Where is my order?: replied
Can I change my address?: waiting for a reply