Skip to content

Core\Imap\Folder::unflag

$folder->unflag(int $uid, string $flag): void$folder->unflag(
    int $uid,
    string $flag,
): void

Removes a flag from one message. It is the opposite of flag.

Pass the UID of the message (the number the server gives each message in a folder) and the flag. The flag is a name such as \Flagged, or a keyword of your own such as $Todo. Only that flag is removed, and the other flags of the message do not change. If the message does not have the flag, nothing changes and no error is thrown.

unflag takes the same flags as flag: \Answered, \Flagged, \Draft, \Seen, or a keyword. To remove \Seen, you can also call markUnseen.

Good to know: as with flag, \Deleted is not allowed and throws a RuntimeError. If no message has that UID, unflag throws an IOError.

Parameters

  • $uidintA UID (the number the server gives each message in a folder) that search returned.
  • $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$uid is less than 1 or larger than 4294967295.
  • RuntimeError$flag starts 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. \Deleted throws too. To delete a message, call delete.
  • IOErrorNo message has that UID, or the server does not answer. It is also thrown when the folder's UIDVALIDITY changed. That is a number that changes when the server numbers the messages again.

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
# 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"]

Remove a flag

<?nvs
// Shows how to remove the flag from a complaint once it is answered.
use Core\IO;
use Core\Imap;
var $inbox = Imap::open('shop')->folder('INBOX');
// A `directory` block is a folder on disk that works like a mailbox, for tests and local work.
// In such a block, the file `1.flags` has the flags of message 1, one per line.
echo "before:\n", IO::readText('shop/INBOX/1.flags');
// This removes the flag `\Flagged` from message 1.
$inbox->unflag(1, '\Flagged');
// `\Seen` does not change. This prints only "\Seen".
echo "after:\n", IO::readText('shop/INBOX/1.flags');
// This adds the flag again, so the example prints the same each time it runs.
$inbox->flag(1, '\Flagged');
Output
before:
\Seen
\Flagged
after:
\Seen

A flag the message does not have

<?nvs
// Shows that `unflag` changes nothing when the message does not have the flag.
use Core\IO;
use Core\Imap;
var $inbox = Imap::open('support')->folder('INBOX');
// Message 1 has no flags. The two calls below remove `\Answered` and `$Urgent`.
// They change nothing, and they do not throw an error.
$inbox->unflag(1, '\Answered');
$inbox->unflag(1, '$Urgent');
// A message without flags has no `1.flags` file. This prints "flags file: no".
echo 'flags file: ', IO::exists('support/INBOX/1.flags') ? 'yes' : 'no', "\n";
// A UID that does not exist throws an `IOError`.
try {
$inbox->unflag(5, '\Answered');
} catch (IOError $error) {
echo $error->message, "\n";
}
Output
flags file: no
Core\Imap\Folder::unflag: the folder has no message with the UID 5

Remove a to do label

<?nvs
// Shows a script that removes the label `$Todo` from an order once the order is sent.
use Core\IO;
use Core\Imap;
var $inbox = Imap::open('orders')->folder('INBOX');
echo 'sent: ', $inbox->fetch(1)->subject(), "\n";
$inbox->unflag(1, '$Todo');
// `$Todo` was the only flag, so the file `1.flags` is deleted. This prints "flags file: no".
echo 'flags file: ', IO::exists('orders/INBOX/1.flags') ? 'yes' : 'no', "\n";
// This adds the label again, so the example prints the same each time it runs.
$inbox->flag(1, '$Todo');
Output
sent: Order: 4 cups
flags file: no