Core\Imap\Folder::flag
Adds a flag to one message. A flag is a label on the message that every mail program can read. You name the message by its UID (the number the server gives each message in a folder).
The standard flags are \Flagged (a star or a red flag in most mail programs), \Answered, \Draft and \Seen. You can also use your own label, a keyword such as $Paid or Invoice. A keyword may start with $, but it does not have to. A keyword has no space and none of the characters (){%*"\]. It is at most 255 bytes long. If the message has the flag already, nothing changes. unflag removes a flag again.
Good to know: \Deleted is not allowed, and flag throws a RuntimeError for it. To delete a message, call delete. delete deletes only the message you name. Any other flag that starts with \ also throws a RuntimeError.
Parameters
$uidintA UID (the number the server gives each message in a folder) thatsearchreturned.$flagstring\Answered,\Flagged,\Draft,\Seen, or a keyword such as$Important. A keyword cannot have a space or any of these characters:(){%*"\].
Return value
Returns void. Nothing.
Errors
RuntimeError$uidis less than 1 or larger than 4294967295.RuntimeError$flagstarts with\and is not one of the four standard flags, or it has a space or one of the characters(){%*"\], or it is longer than 255 bytes.\Deletedthrows too. To delete a message, calldelete.IOErrorNo message has that UID, or the server does not answer. It is also thrown when the folder'sUIDVALIDITYchanged. That is a number that changes when the server numbers the messages again.
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.
# Each example reads its own mailbox, a folder beside this file. They need no mail server.[imap.shop]directory = "shop"
[imap.support]directory = "support"
[imap.orders]directory = "orders"
# The program may open the three blocks.[capabilities.imap]connect = ["shop", "support", "orders"]Flag a message
<?nvs// Shows how to flag a complaint, so that it shows with a star or a red flag in a mail program.
use Core\IO;use Core\Imap;
var $inbox = Imap::open('shop')->folder('INBOX');
$inbox->flag(1, '\Flagged');
// A `directory` block is a folder on disk that works like a mailbox, for tests and local work.// Its file `1.flags` has the flags of message 1, one per line. This prints "\Flagged".echo IO::readText('shop/INBOX/1.flags');
// This removes the flag again, so the example prints the same each time it runs.$inbox->unflag(1, '\Flagged');\FlaggedA label of your own
<?nvs// Shows how to add a label of your own, `$Paid`, to a message about a payment.
use Core\IO;use Core\Imap;
var $inbox = Imap::open('orders')->folder('INBOX');
// The second call changes nothing, because the message has the label already.$inbox->flag(1, '$Paid');$inbox->flag(1, '$Paid');
// The message was read before, so it has two flags. This prints "\Seen" and "$Paid".echo IO::readText('orders/INBOX/1.flags');
// This removes the label again, so the example prints the same each time it runs.$inbox->unflag(1, '$Paid');\Seen$PaidFlags that are not allowed
<?nvs// Shows the flags that `flag` does not allow. Each one throws a `RuntimeError`.
use Core\Imap;
var $inbox = Imap::open('support')->folder('INBOX');
// To delete a message, call `delete` instead.try { $inbox->flag(1, '\Deleted');} catch (RuntimeError $deleted) { echo $deleted->message, "\n";}
// A keyword cannot have a space.try { $inbox->flag(1, 'not spam');} catch (RuntimeError $space) { echo $space->message, "\n";}
// A keyword can have a `-`, so this line works.$inbox->flag(1, 'Not-Spam');echo "Not-Spam is set\n";
// This removes the flag again, so the example prints the same each time it runs.$inbox->unflag(1, 'Not-Spam');Core\Imap\Folder::flag: `\Deleted` cannot be set as a flag. To delete a message, call `delete`.Core\Imap\Folder::flag: `not spam` cannot be set as a flag. A keyword has no space and none of the characters `(){%*"\]`.Not-Spam is set