use the UID message to determine the last message, you have to store the last UID in the table
$uidsArray = imap_sort($imapConnection, SORTARRIVAL, 1, SE_UID);
if ($uidsArray) {
// read UID last message, XEmailUID - table(mailbox, lastuid mailbox)
$lastUIDObject = new XEmailUID();
$lastUIDObject->setImap($mailbox->getId().'/'.$mailboxRef);
if (!$lastUIDObject->select()) {
$lastUIDObject->insert();
}
$uidMax = 0;
foreach ($uidsArray as $uid) {
if ($uid < $lastUIDObject->getUid()) {
continue;
}
if ($uid >= $uidMax) {
$uidMax = $uid;
}
// your function
$this->_readIMAPMessage(
$imapConnection,
$uid,
$mailboxRef
);
}
if ($uidMax > 0) {
$lastUIDObject->setUid($uidMax);
$lastUIDObject->update();
}
}