Core\Imap\Folder::fetch
$folder->fetch(int $uid): Core\Mail\Message$folder->fetch( int $uid, ): Core\Mail\Message
Reads one message of a folder and returns it as a Core\Mail\Message.
Pass a UID (the number the server gives each message in a folder) that search returned. The message has the same methods as one that Core\Mail\Message::parse returns: subject, from, date, text, attachments, kind and the rest. Its uid() is the UID you passed. Every part of the message comes from its sender, so each text is a tainted string. A tainted string is text from outside your program.
fetch does not mark the message as read. Call markSeen when your program is done with it.
Good to know: if no message has that UID, fetch throws an IOError. It also throws an IOError when the message is larger than the memory the request has left. The body of such a message is not read.
Parameters
$uidintA UID (the number the server gives each message in a folder) thatsearchreturned.
Return value
Returns Core\Mail\Message. The parsed message. Its uid() is $uid.
Errors
RuntimeError$uidis less than 1 or larger than 4294967295.IOErrorNo message has that UID, or the server does not answer. It is also thrown when the message is larger than the memory the request has left. 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.
# 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"]Read a message
<?nvs// Shows how to read one message and print its sender, date, subject and text.
use Core\Imap;
var $inbox = Imap::open('inbox')->folder('INBOX');var $message = $inbox->fetch(1);
echo 'From: ', $message->from()?->name() ?? 'unknown', ' <', $message->from()?->address() ?? '', ">\n";echo 'Date: ', $message->date()?->format('yyyy-MM-dd HH:mm') ?? 'no date', "\n";echo 'Subject: ', $message->subject(), "\n\n";echo $message->text() ?? '', "\n";From: Ada Lovelace <ada@example.com>Date: 2026-11-02 09:15Subject: The lamp does not switch on
Hello,the lamp from order 1041 does not switch on.Can you send a new one?Fetch does not mark as read
<?nvs// Shows that `fetch` does not mark a message as read, and what happens for a UID that does not exist.
use Core\Arr;use Core\Imap;
var $inbox = Imap::open('inbox')->folder('INBOX');
// The messages with the UIDs 2 and 3 are not read yet. So there are 2 unread messages.// This prints "not read: 2" before and after `fetch(2)` reads message 2.echo 'not read: ', Arr::count($inbox->search({ unseen: true })), "\n";$inbox->fetch(2);echo 'not read: ', Arr::count($inbox->search({ unseen: true })), "\n";
// The folder has no message with the UID 9, so `fetch` throws an `IOError`.try { $inbox->fetch(9);} catch (IOError $error) { echo $error->message, "\n";}not read: 2not read: 2Core\Imap\Folder::fetch: the folder has no message with the UID 9A support desk lists new requests
<?nvs// Shows a support desk script that lists each new request with its sender and the first line of its text.
use Core\Imap;use Core\Str;
var $inbox = Imap::open('inbox')->folder('INBOX');
foreach ($inbox->search({ unseen: true }) as int $uid) { var $message = $inbox->fetch($uid); // `uid()` returns the UID that was passed to `fetch`. string $ticket = 'T-' . ($message->uid() ?? 0); var $firstLine = Str::split($message->text() ?? '', "\n")[0] ?? ''; echo $ticket, ' ', $message->from()?->address() ?? 'unknown', ': ', $firstLine, "\n";}T-2 bo@example.org: I ordered a blue chair and got a red one.T-3 cy@example.net: Is your shop open on Sunday?