Core\Imap\Folder::uidValidity
$folder->uidValidity(): intReturns the UIDVALIDITY number of a folder. The number tells you if the UIDs you saved for this folder are still correct.
Each message in a folder has a UID (the number the server gives each message in a folder). It stays the same while the message is in the folder. A script that runs again later can save the highest UID it has read, and then read only the newer messages. Save the UIDVALIDITY with it. Sometimes a server numbers the messages of a folder again, for example after a restore. Then it gives the folder a new UIDVALIDITY, and a UID you saved may now belong to another message.
In plain words: the UIDs are like seat numbers, and the UIDVALIDITY is the date of the event. A seat number from another event is no help.
Good to know: a directory block is a folder on disk that works like a mailbox, for tests and local work. In such a block, the number is always 1.
Return value
Returns int. A number of 1 or more.
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 the uid validity
<?nvs// Shows how to read the `UIDVALIDITY` of a folder.
use Core\Imap;
var $inbox = Imap::open('inbox')->folder('INBOX');
// A `directory` block is a folder on disk that works like a mailbox, for tests and local work.// Such a block always returns 1. A mail server returns its own number.echo 'UIDVALIDITY of INBOX: ', $inbox->uidValidity(), "\n";UIDVALIDITY of INBOX: 1Read only the new messages
<?nvs// Shows how a script reads only the messages that are newer than the last run.
use Core\Imap;
// The last run saved two numbers. The `UIDVALIDITY` was 1.// The highest UID it read was also 1.int $savedValidity = 1;int $lastUid = 1;
var $inbox = Imap::open('inbox')->folder('INBOX');
// The `UIDVALIDITY` is the same, so the saved UID is still correct.if ($inbox->uidValidity() == $savedValidity) { foreach ($inbox->search() as int $uid) { if ($uid > $lastUid) { echo 'new: ', $uid, ' ', $inbox->fetch($uid)->subject(), "\n"; $lastUid = $uid; } }}
// The script saves this line for its next run. This prints "save: 1 3".echo 'save: ', $inbox->uidValidity(), ' ', $lastUid, "\n";new: 2 Can I change my address?new: 3 Thank yousave: 1 3Start again when the number changes
<?nvs// Shows what a script does when the `UIDVALIDITY` of a folder is not the one it saved.
use Core\Imap;
// An earlier run saved the `UIDVALIDITY` 7 and the UID 2.int $savedValidity = 7;int $lastUid = 2;
var $inbox = Imap::open('inbox')->folder('INBOX');
// The folder now has the `UIDVALIDITY` 1. So the saved UID 2 may now belong to another message.// The script forgets the saved UID and reads every message again.if ($inbox->uidValidity() != $savedValidity) { echo 'UIDVALIDITY changed from ', $savedValidity, ' to ', $inbox->uidValidity(), "\n"; $lastUid = 0;}
foreach ($inbox->search() as int $uid) { if ($uid > $lastUid) { echo 'read: ', $uid, ' ', $inbox->fetch($uid)->subject(), "\n"; }}UIDVALIDITY changed from 7 to 1read: 1 Where is my order?read: 2 Can I change my address?read: 3 Thank you