note 104057 added to function.imap-thread

From: Date: Fri, 20 May 2011 04:09:03 +0000
Subject: note 104057 added to function.imap-thread
Groups: php.notes 
Request: Send a blank email to php-notes+get-179894@lists.php.net to get a copy of this message
so if you are going to use imap_thread() for something useful. This is probably the most optimal way I can think of: <?php $imap = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', 'youremail@gmail.com', 'password'); $threads = $rootValues = array(); $thread = imap_thread($imap); $root = 0; //first we find the root (or parent) value for each email in the thread //we ignore emails that have no root value except those that are infact //the root of a thread //we want to gather the message IDs in a way where we can get the details of //all emails on one call rather than individual calls ( for performance ) //foreach thread foreach ($thread as $i => $messageId) { //get sequence and type list($sequence, $type) = explode('.', $i); //if type is not num or messageId is 0 or (start of a new thread and no next) or is already set if($type != 'num' || $messageId == 0 || ($root == 0 && $thread[$sequence.'.next'] == 0) || isset($rootValues[$messageId])) { //ignore it continue; } //if this is the start of a new thread if($root == 0) { //set root $root = $messageId; } //at this point this will be part of a thread //let's remember the root for this email $rootValues[$messageId] = $root; //if there is no next if($thread[$sequence.'.next'] == 0) { //reset root $root = 0; } } //now get all the emails details in rootValues in one call //because one call for 1000 rows to a server is better //than calling the server 1000 times $emails = imap_fetch_overview($imap, implode(',', array_keys($rootValues))); //foreach email foreach ($emails as $email) { //get root $root = $rootValues[$email->msgno]; //add to threads $threads[$root][] = $email; } //there is no need to sort, the threads will automagically in chronological order echo '<pre>'.print_r($threads, true).'</pre>'; imap_close($imap); exit; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 112.204.98.31 ---- Manual Page -- http://www.php.net/manual/en/function.imap-thread.php Edit -- https://master.php.net/note/edit/104057 Del: integrated -- https://master.php.net/note/delete/104057/integrated Del: useless -- https://master.php.net/note/delete/104057/useless Del: bad code -- https://master.php.net/note/delete/104057/bad+code Del: spam -- https://master.php.net/note/delete/104057/spam Del: non-english -- https://master.php.net/note/delete/104057/non-english Del: in docs -- https://master.php.net/note/delete/104057/in+docs Del: other reasons-- https://master.php.net/note/delete/104057 Reject -- https://master.php.net/note/reject/104057 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179894) next »