PHP Requires Chilkat v11.0.0+
PHP
Fetch Oldest/Newest IMAP Email
Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.Chilkat PHP Downloads
<?php
use Chilkat2\Email;
use Chilkat2\Imap;
// This example assumes 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('mail.testemail.net');
// Login
$imap->Login('***', '***');
// Select an IMAP mailbox
$imap->SelectMailbox('Inbox');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// After selecting a mailbox, the NumMessages property
// contains the number of emails in the selected mailbox.
$n = $imap->NumMessages;
if ($n > 0) {
// The oldest email is always at sequence number 1.
$isUid = false;
$oldestEmail = new Email();
try {
$imap->FetchEmail(false, 1, $isUid, $oldestEmail);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Display the From and Subject
echo $oldestEmail->FromAddress, "\n";
echo $oldestEmail->Subject, "\n";
echo "--\n";
// The newest email is at sequence number N:
$newestEmail = new Email();
try {
$imap->FetchEmail(false, $n, $isUid, $newestEmail);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Display the From and Subject
echo $newestEmail->FromAddress, "\n";
echo $newestEmail->Subject, "\n";
}
// Disconnect from the IMAP server.
$imap->Disconnect();