I am building my own webmail client. Like Roundcube or Squirrelmail, for example. The problem is that my version is very slow, while Roundcube is fast and I cannot understand why is that (Roundcube's source ir very big and I am unable to dive in it..)
The goal - fetch last 50 messages from mailbox. My strategy:
Get number of messages in mailbox by imap_num_msg()
Make array of sequence numbers from max to (max-50)
For each sequence number I ran functions imap_header() and imap_fetchstructure()
It takes 10-15 seconds. It allows me to get each messages title, date, whether is has attachments or not, from, to and other information.
However, Roundcube displays the same info, but load time is only 3 seconds or so. My strategy seems to be very wrong. How can I do it faster? I'm pretty sure that it must be slow to ran imap_header and imap_fetchstructure for each sequence number, but I think there is no other way to get that information.. I'm doing something like this:
function getMessageBySequenceNumber($imapStream, $sequence_number){
$header = imap_header($imapStream, $sequence_number);
$structure = imap_fetchstructure($imapStream, $sequence_number);
/*
... some code parsing $structure to find out whether this emails has any attachments or not
*/
return [
'uid' => imap_uid($imapStream, $i),
'subject' => $header->subject,
'timestamp' => $header->udate,
'unseen' => $header->Unseen,
'star' => $header->Flagged,
'draft' => $header->Draft,
'size' => $header->size,
'has_attachments_bool' => $has_attachments_bool,
];
}
$imapStream = imap_open();
$first_sequence_number = imap_num_msg(); // lets imagine it returns 100
$last_sequence_number = $first_sequence_number-50;
$sequence_numbers = [100,99,88 ..., 51, 50];
$messages = [];
foreach($sequence_numbers as $sequence_number){
$messages[] = getMessageBySequenceNumber($imapStream, $sequence_number);
}
return $messages;