Skip to content

Commit db7ac7c

Browse files
SiriuoWebklex
authored andcommitted
This should do the job to limit the number of messages fetched from the server. (#96)
Tested with my personal mailbox which has thousands of emails and all appears to work as normal. $folder->getMessages('ALL', null, false, false, $number_of_results, $page_number);
1 parent 44aa0c3 commit db7ac7c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/IMAP/Folder.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public function getMessage($uid, $msglist = null, $fetch_options = null, $fetch_
177177
* @throws GetMessagesFailedException
178178
* @throws MessageSearchValidationException
179179
*/
180-
public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true) {
181-
return $this->searchMessages([[$criteria]], $fetch_options, $fetch_body, 'UTF-8', $fetch_attachment);
180+
public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $limit = null, $page = 1) {
181+
return $this->searchMessages([[$criteria]], $fetch_options, $fetch_body, $fetch_attachment, $limit, $page);
182182
}
183183

184184
/**
@@ -262,7 +262,7 @@ public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $
262262
* / ( ("+" / "-") 4DIGIT ) ; Local differential
263263
* ; hours+min. (HHMM)
264264
*/
265-
public function searchMessages(array $where, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true) {
265+
public function searchMessages(array $where, $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $limit = null, $page = 1, $charset = "UTF-8") {
266266

267267
$this->getClient()->checkConnection();
268268

@@ -290,15 +290,19 @@ public function searchMessages(array $where, $fetch_options = null, $fetch_body
290290

291291
$query = trim($query);
292292

293-
$availableMessages = imap_search($this->getClient()->getConnection(), $query, SE_UID, $charset);
293+
$availableMessages = array_reverse(imap_search($this->getClient()->getConnection(), $query, SE_UID, $charset));
294294

295+
$numMessages = imap_num_msg($this->getClient()->getConnection());
296+
if($limit == null || $limit > $numMessages){
297+
$maxMessages = $numMessages;
298+
}else{
299+
$maxMessages = $limit;
300+
}
295301
if ($availableMessages !== false) {
296-
$msglist = 1;
297-
foreach ($availableMessages as $msgno) {
302+
for ($msglist = ($page * $maxMessages) - $limit; $msglist < $page * $maxMessages; $msglist++){
303+
$msgno = $availableMessages[$msglist];
298304
$message = new Message($msgno, $msglist, $this->getClient(), $fetch_options, $fetch_body, $fetch_attachment);
299-
300305
$messages->put($message->getMessageId(), $message);
301-
$msglist++;
302306
}
303307
}
304308

0 commit comments

Comments
 (0)