SSilence / php-imap-client

a easy solution for simple IMAP email access in php
MIT License
268 stars 138 forks source link

getMessage($id) returns wrong message #220

Closed geri777 closed 6 years ago

geri777 commented 6 years ago

When I pull the last 20 messages and then want to access a specific message I get the wrong one. The message I get is not within the 20 messages of $allEmails. I am using POP3 not IMAP.

$allEmails = $imap->getMessages(20);
$cntr = 1;
foreach($allEmails As &$email) {
     // add the ID to each email
     $email['id] = $cntr;
     $cntr++;
}
var_dump($allEmails);

//get the Message No. 3 
var_dump($imap->getMessage(3));
mattparksjr commented 6 years ago

Arrays start at 0 not 1. If you want to access #3 you need to request #4

geri777 commented 6 years ago

I thought it is 1-based, because when I look in function checkMessageId($id) I find ... if($id > $this->countMessages()){ throw new ImapClientException('Bad message number'); }

But anyway - THIS IS NOT THE PROBLEM unfortunately. I get a very old message (maybe number 500 or so), which is not part of the $imap->getMessages(20).

mattparksjr commented 6 years ago

Lets take for example an array with 49 messages. Count messages would return 50. But if you ask for the number 50, the exception is thrown. Get it?

On the part about the message, just grab that section

geri777 commented 6 years ago

If you have 50 messages, count will return 50, wouldn't it? If you have a Null-based index the highest id can be 49. In this case the checkMessageId would be if($id >= $this->countMessages()) (note the equal sign). Please post the part with "grab that section" again. If I click it it's empty. Many thanks!

geri777 commented 6 years ago

I now understand what's going wrong. I am creating a new array in my foreach loop - with a new index. I had to use the old index IDs to access the single messages:

$allEmails = $imap->getMessages(20);
foreach($allEmails As &$email) {
     // I want to get the message with "Hello" in the subject
     if($email['subject'] = 'Hello' {
        var_dump($imap->getMessage($email['uid']));
     }
}