PHP Requires Chilkat v11.0.0+
PHP
Sorting Email
Demonstrates how to sort an email bundle.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 the IMAP server.
// Use TLS
$imap->Ssl = true;
$imap->Port = 993;
try {
$imap->Connect('imap.example.com');
// Login
$imap->Login('myLogin', 'myPassword');
// Select an IMAP mailbox
$imap->SelectMailbox('Inbox');
$fetchUids = true;
$messageSet = new MessageSet();
$imap->QueryMbx('ALL', $fetchUids, $messageSet);
// Fetch the email headers into a bundle object:
$bundle = new EmailBundle();
$headersOnly = true;
$imap->FetchMsgSet($headersOnly, $messageSet, $bundle);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Sort the email bundle by date, recipient, sender, or subject:
$ascending = true;
$bundle->SortByDate($ascending);
// To sort by recipient, sender, or subject, call
// SortBySender, SortByRecipient, or SortBySubject.
// Display the Subject and From of each email.
$email = new Email();
$i = 0;
while ($i < $bundle->MessageCount) {
$bundle->EmailAt($i, $email);
echo $email->GetHeaderField('Date'), "\n";
echo $email->Subject, "\n";
echo $email->From, "\n";
echo "--\n";
$i = $i + 1;
}
// Disconnect from the IMAP server.
$imap->Disconnect();