Skip to content

Core\Imap\Folder::search

$folder->search({…}): array<int>$folder->search(
    {…},
): array<int>

Finds messages in a folder and returns their UIDs, the oldest first. A UID is the number the server gives each message in a folder.

Set one or more options, and search returns the messages that match all of them. unseen: true finds the messages that are not read yet. since and before take a Core\Time\Date. since includes that day, and before does not. from and subject find the messages whose From header or subject contains the text. Upper and lower case do not matter. limit returns at most that many UIDs. With no option, search returns every message in the folder.

Pass each UID to fetch to read the message, or to move, markSeen or delete to change it.

Good to know: search does not read the messages and does not mark them as read. A directory block is a folder on disk that works like a mailbox, for tests and local work. In such a block, since and before use the Date header of each message. A message whose date cannot be read matches neither since nor before.

Options

  • unseenbooloptionaltrue finds only the messages that are not read yet.
  • sinceCore\Time\DateoptionalFinds the messages received on this day or later.
  • beforeCore\Time\DateoptionalFinds the messages received before this day. The day itself is not included.
  • fromstringoptionalFinds the messages whose From header contains this text.
  • subjectstringoptionalFinds the messages whose subject contains this text.
  • limitintoptionalReturns at most this many UIDs, the oldest first.

Return value

Returns array<int>. The UIDs, oldest first.

Errors

  • RuntimeErrorlimit is less than 1.
  • IOErrorThe server does not answer, or 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
# 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"]

The unseen messages since a date

<?nvs
// Shows how to find the messages that are not read yet and arrived on 1 November or later.
use Core\Imap;
use Core\Time\Date;
var $inbox = Imap::open('inbox')->folder('INBOX');
// Both options must match. Message 2 is not read, but it arrived in October.
foreach ($inbox->search({ unseen: true, since: Date::at(2026, 11, 1) }) as int $uid) {
echo $uid, ' ', $inbox->fetch($uid)->subject(), "\n";
}
Output
3 INVOICE 1042 IS WRONG
5 Where is my parcel?

Search by sender and subject

<?nvs
// Shows how to find messages by a part of the sender and a part of the subject.
use Core\Imap;
var $inbox = Imap::open('inbox')->folder('INBOX');
// The case of the letters does not matter, so `invoice` also finds "INVOICE 1042 IS WRONG".
echo "subject contains 'invoice':\n";
foreach ($inbox->search({ subject: 'invoice' }) as int $uid) {
echo ' ', $uid, ' ', $inbox->fetch($uid)->subject(), "\n";
}
// With two options, a message must match both. Ada sent messages 1 and 4, and only 1 is about an invoice.
echo "from Ada, about an invoice:\n";
foreach ($inbox->search({ from: 'ada@example.com', subject: 'invoice' }) as int $uid) {
echo ' ', $uid, ' ', $inbox->fetch($uid)->subject(), "\n";
}
Output
subject contains 'invoice':
1 Invoice 1039
3 INVOICE 1042 IS WRONG
from Ada, about an invoice:
1 Invoice 1039

A cleanup job handles a few at a time

<?nvs
// Shows a cleanup job that finds the old messages and handles at most two per run.
use Core\Imap;
use Core\Time\Date;
var $inbox = Imap::open('inbox')->folder('INBOX');
// `before` does not include the day itself, so messages 1, 2 and 3 match.
// `limit: 2` returns the two oldest of these three messages.
foreach ($inbox->search({ before: Date::at(2026, 11, 3), limit: 2 }) as int $uid) {
var $message = $inbox->fetch($uid);
echo $uid, ' ', $message->date()?->format('yyyy-MM-dd') ?? 'no date', ' ', $message->subject(), "\n";
}
// A limit of 0 is not allowed, so `search` throws a `RuntimeError`.
try {
$inbox->search({ limit: 0 });
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
1 2026-10-28 Invoice 1039
2 2026-10-30 Our offers for November
Core\Imap\Folder::search: `limit` is 0, and it must be 1 or more