Skip to content

Core\Imap\Folder::markUnseen

$folder->markUnseen(int $uid): void$folder->markUnseen(
    int $uid,
): void

Marks one message as not read. It removes the \Seen flag of the message.

After markUnseen, a mail program shows the message as new again, and search({ unseen: true }) finds it. A script uses this when it took a message but could not finish the work, so the next run tries the message again. A person can also use it for a message they want to read again later. The message then shows as new in their mail program.

Only \Seen is removed. Other flags of the message, such as \Flagged, do not change. If the message is not read, markUnseen changes nothing. You name the message by its UID (the number the server gives each message in a folder). If no message has that UID, markUnseen throws an IOError.

Parameters

  • $uidintA UID (the number the server gives each message in a folder) that search returned.

Return value

Returns void. Nothing.

Errors

  • RuntimeError$uid is less than 1 or larger than 4294967295.
  • 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"]

Mark a message as not read

<?nvs
// Shows how to mark a read message as not read, so that it shows as new again.
use Core\Imap;
var $inbox = Imap::open('shop')->folder('INBOX');
// Both messages are read. Nobody has answered message 2 yet, so the script marks it as new.
$inbox->markUnseen(2);
// This prints "new: 2 Can I change my address?".
foreach ($inbox->search({ unseen: true }) as int $uid) {
echo 'new: ', $uid, ' ', $inbox->fetch($uid)->subject(), "\n";
}
// This marks the message as read again, so the example prints the same each time it runs.
$inbox->markSeen(2);
Output
new: 2 Can I change my address?

Try a failed order again

<?nvs
// Shows a script that marks an order as new again when it cannot handle it, so the next run tries again.
use Core\Imap;
use Core\Str;
var $inbox = Imap::open('orders')->folder('INBOX');
foreach ($inbox->search({ unseen: true }) as int $uid) {
// The script marks the order as read first, so a second script does not handle the same order.
$inbox->markSeen($uid);
var $subject = $inbox->fetch($uid)->subject();
if (Str::contains($subject, 'sofa')) {
// The shop has no sofa today. The order is marked as new, and the next run tries it again.
echo 'not sent: ', $subject, "\n";
$inbox->markUnseen($uid);
} else {
echo 'sent: ', $subject, "\n";
}
}
// This prints "next run: 2".
foreach ($inbox->search({ unseen: true }) as int $uid) {
echo 'next run: ', $uid, "\n";
}
// This marks order 1 as new again, so the example prints the same each time it runs.
$inbox->markUnseen(1);
Output
sent: Order: 2 lamps
not sent: Order: 1 sofa
next run: 2

Other flags stay

<?nvs
// Shows that `markUnseen` removes only `\Seen`, and the other flags of the message stay.
use Core\IO;
use Core\Imap;
var $inbox = Imap::open('support')->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('support/INBOX/1.flags');
$inbox->markUnseen(1);
// The message is still `\Flagged`. This prints only "\Flagged".
echo "after:\n", IO::readText('support/INBOX/1.flags');
// This marks the message as read again, so the example prints the same each time it runs.
$inbox->markSeen(1);
Output
before:
\Flagged
\Seen
after:
\Flagged