PHP Requires Chilkat v11.0.0+
PHP
Get IMAP UID from Email Header
See more IMAP Examples
Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.Chilkat PHP Downloads
<?php
use Chilkat2\Email;
use Chilkat2\EmailBundle;
use Chilkat2\Imap;
use Chilkat2\MessageSet;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
$imap = new Imap();
// Connect to an IMAP server.
// Use TLS
$imap->Ssl = true;
$imap->Port = 993;
try {
$imap->Connect('imap.example.com');
// Login
$imap->Login('login', 'password');
// Select an IMAP mailbox
$imap->SelectMailbox('Inbox');
$fetchUids = true;
// Get the message UIDs of all the emails in the mailbox
$messageSet = new MessageSet();
$imap->QueryMbx('ALL', $fetchUids, $messageSet);
$bundle = new EmailBundle();
$headersOnly = true;
$imap->FetchMsgSet($headersOnly, $messageSet, $bundle);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// The UID of each fetched email header is available
// in the ckx-imap-uid header field.
$email = new Email();
$msgSet1 = new MessageSet();
$i = 0;
$szBundle = $bundle->MessageCount;
while ($i < $szBundle) {
$bundle->EmailAt($i, $email);
// Build a message set containing one UID
$uidStr = $email->GetHeaderField('ckx-imap-uid');
$msgSet1->FromCompactString($uidStr);
// Move this message to some other folder.
try {
$imap->MoveMessages($msgSet1, 'someOtherFolder');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
// Exit the loop...
$i = $szBundle;
}
$i = $i + 1;
}
// Disconnect from the IMAP server.
$imap->Disconnect();