Skip to content

Core\Imap\Folder::markSeen

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

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

A mail program shows a read message in normal letters and a new one in bold. search({ unseen: true }) finds only the messages without \Seen. A script that handles each new message once calls markSeen after it handles the message. On its next run, search no longer finds that message.

fetch does not mark a message as read, so your program decides when a message counts as read. If the message is read already, markSeen 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, markSeen throws an IOError.

Good to know: call markSeen after the work is done. If the work fails first, the message is still new, and the next run tries it again.

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 read

<?nvs
// Shows how to mark a message as read, and how the number of new messages that `search` finds changes.
use Core\Arr;
use Core\Imap;
var $inbox = Imap::open('shop')->folder('INBOX');
// This prints "new: 2".
echo 'new: ', Arr::count($inbox->search({ unseen: true })), "\n";
$inbox->markSeen(1);
// Message 1 is read now, so this prints "new: 1".
echo 'new: ', Arr::count($inbox->search({ unseen: true })), "\n";
// This marks the message as new again, so the example prints the same each time it runs.
$inbox->markUnseen(1);
Output
new: 2
new: 1

Marking twice changes nothing

<?nvs
// Shows that `markSeen` on a message that is read already changes nothing.
use Core\Arr;
use Core\Imap;
var $inbox = Imap::open('support')->folder('INBOX');
// Message 1 is read already, and message 2 is marked twice. These three calls do not throw an error.
$inbox->markSeen(1);
$inbox->markSeen(2);
$inbox->markSeen(2);
// Messages 1 and 2 are both read now, so this prints "new: 0".
echo 'new: ', Arr::count($inbox->search({ unseen: true })), "\n";
// A message that does not exist throws an `IOError`.
try {
$inbox->markSeen(7);
} catch (IOError $error) {
echo $error->message, "\n";
}
// This marks message 2 as new again, so the example prints the same each time it runs.
$inbox->markUnseen(2);
Output
new: 0
Core\Imap\Folder::markSeen: the folder has no message with the UID 7

Handle each new order once

<?nvs
// Shows a script that handles each new order once. It runs twice here, and the second run finds nothing new.
use Core\Imap;
var $inbox = Imap::open('orders')->folder('INBOX');
array<int> $handled = [];
foreach ([1, 2] as var $run) {
echo 'run ', $run, ":\n";
foreach ($inbox->search({ unseen: true }) as int $uid) {
echo ' ', $inbox->fetch($uid)->subject(), "\n";
// The order is handled, so the script marks the message as read.
$inbox->markSeen($uid);
$handled[] = $uid;
}
}
// These lines mark the orders as new again, so the example prints the same each time it runs.
foreach ($handled as int $uid) {
$inbox->markUnseen($uid);
}
Output
run 1:
Order: 2 lamps
Order: 4 cups
run 2: