PHP
PHP
Determine the Number of Unseen Email Messages
Demonstrates how to determine how many unseen messages exist in an email account on an IMAP server.Chilkat PHP Downloads
<?php
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('***', '***');
// Select an IMAP mailbox
$imap->SelectMailbox('Inbox');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// After selecting the mailbox. the total number of emails
// is immediately available:
$totalNum = $imap->NumMessages;
echo 'Num messages = ', $totalNum, "\n";
// To determine the number of unseen messages, a call
// to Search is required, which returns the set of UIDs
// of all unseen messages.
// We can choose to fetch UIDs or sequence numbers.
$fetchUids = true;
$messageSet = new MessageSet();
try {
$imap->QueryMbx('UNSEEN', $fetchUids, $messageSet);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$numUnseen = $messageSet->Count;
echo $numUnseen, "\n";
echo "UIDs ----\n";
// Display the UIDs
$i = 0;
while ($i < $messageSet->Count) {
echo $messageSet->GetId($i), "\n";
$i = $i + 1;
}
// Disconnect from the IMAP server.
$imap->Disconnect();