Core\Mail\Message::uid
$message->uid(): ?intReturns the UID of a message that was read from a mailbox folder, or null for a message that Core\Mail\Message::parse read.
A UID is the number of a message in its mailbox folder. It does not change while the message stays in that folder. Core\Imap\Folder::search returns the UIDs of a folder, and Core\Imap\Folder::fetch reads the message with one UID. That message's uid() is the same number.
Pass the UID to the other methods of the same folder, such as move, markSeen or delete. A UID belongs to one folder. When a message is moved, it gets a new UID in the other folder.
The examples below show the UID of each message in a folder, a parsed message with no UID, and how to keep the UIDs of the bounces in a folder. A bounce is a message from a mail server that says your mail could not be delivered.
Return value
Returns ?int. The UID, or null for a message that parse read.
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 mailbox `inbox` is the folder `mailbox` beside this file. Each folder in it is a# mailbox folder, and each `<uid>.eml` file in a folder is one message.[imap.inbox]directory = "mailbox"
# The program may open the mailbox `inbox`.[capabilities.imap]connect = ["inbox"]
# The program may read the files of the mailbox, and no other file.[capabilities.fs]read = ["mailbox"]The uid of each message
<?nvs// Shows the UID of each message that is read from a mailbox folder.
use Core\Mail\Message;
var $inbox = Core\Imap::open('inbox')->folder('INBOX');
// `search()` returns the UIDs of the folder, the oldest first.// `fetch()` reads one message, and its `uid()` is the UID it was read with.foreach ($inbox->search() as int $uid) { Message $message = $inbox->fetch($uid); echo $message->uid() ?? 0, ': ', $message->subject(), "\n";}3: Where is my order?7: Undelivered Mail Returned to Sender12: Re: Your orderA parsed message has no uid
<?nvs// Shows that only a message read from a mailbox folder has a UID.
use Core\Mail\Message;
// This message is read from bytes, not from a folder, so `uid()` returns `null`.Message $parsed = Message::parse("Subject: Hello\n\nHi!");echo $parsed->uid() ?? 'null', "\n";
// This message is read from the folder `INBOX`. This prints `3`, the UID of the message.Message $fetched = Core\Imap::open('inbox')->folder('INBOX')->fetch(3);echo $fetched->uid() ?? 'null', "\n";null3Collect the bounces of a folder
<?nvs// Shows how to keep the UIDs of some messages, to pass them to the methods of the folder later.
use Core\Imap\Folder;use Core\Mail\Message;
class Bounces{ // A bounce is a message from a mail server that says your mail could not be delivered. // This function returns every bounce in the folder. public static function in(Folder $folder): array<Message> { array<Message> $found = []; foreach ($folder->search() as int $uid) { Message $message = $folder->fetch($uid); if ($message->kind() == Message::Kind::Bounce) { $found[] = $message; } } return $found; }}
// Each message still has its UID. A program can pass it to `move`, `markSeen` or `delete`.foreach (Bounces::in(Core\Imap::open('inbox')->folder('INBOX')) as Message $bounce) { echo 'bounce ', $bounce->uid() ?? 0, ' for ', $bounce->answers() ?? 'an unknown message', "\n";}bounce 7 for order-42@example.com