(First time programming in PHP. Had some help. Need a bit more.)
Goal:
Pull the lastContactDate from a given email address from my gmail accou
I have never used imap functions, but looking through the manual, the problem might be that your imap_search function is returning simple message sequence numbers instead of UIDs, which are I am guessing, unique message ids?
Maybe someone can help you better I'm just taking a shot with a few things to try.
Try changing your imap_search function to this:
$emails = imap_search($conn,'FROM "'.$findemail.'"', SE_UID);
And your fetch functions to these:
$overview = imap_fetch_overview($conn,$email_number, FT_UID);
$message = imap_fetchbody($conn,$email_number,2, FT_UID);
If that doesn't work, another thing you may try is just to set fetch_overview to one of these instead:
$overview = imap_fetch_overview($conn,"1:{$email_number}",0);
// Or Maybe:
$overview = imap_fetch_overview($conn,"{$email_number}:{$email_number}",0);
Which tells it to grab messages from 1 to, whatever $email_number is I believe, a sequence of message ids rather that Unique Message Ids. Not sure though.
I don't think rsort() will work using the UID method, so you would have to find a different way to sort them, if you used that method. You would probably have to grab an array of all the matching email's headers, and sort that way.
Sorry I am not more helpful, never used imap before, but good luck!
Edit: The man pages are very strange for this, but it looks like the imap_sort function also has search criteria, so in theory, you could do:
$emails = imap_sort($conn, SORTARRIVAL, 0, SE_UID, 'FROM "'.$findemail.'"');
// and then grab the first one:
$emails = array_slice($emails,0,1);
//And then further down use these two with the UID param
$overview = imap_fetch_overview($conn,$email_number, FT_UID);
$message = imap_fetchbody($conn,$email_number,2, FT_UID);
If you are still not getting messages from your archive, you might look at this answer:
PHP imap_search not detecting all messages in gmail inbox
Edit Again
Wow there is really more to this than I thought....This is turning into the longest answer ever...
Depending on what your requirements are, if you only ever need to find messages in the archive folder, I believe you need to reopen the connection and connect to that specific folder before searching, something like:
imap_reopen($conn, "{$gmailhostname}Archive") or die(implode(", ", imap_errors()));
//You can find out what folders are available with this command:
print_r(imap_list($conn, $gmailhostname, '*'));
If you need to search all your folders...That is harder from what I have seen: You either need to loop through each email inbox that you want to search, or find a way to use this:
http://code.google.com/apis/gmail/imap/#x-gm-raw
I think you would need a custom imap handler, or ZEND. Custom IMAP command in php
That is officially all the info I could find.