note 104056 added to function.imap-thread
| From: | cblanquera at gmail dot com | Date: | Fri, 20 May 2011 03:13:49 +0000 |
| Subject: | note 104056 added to function.imap-thread | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179893@lists.php.net to get a copy of this message | ||
imap_thread() returns threads, but are confined to the current open mailbox you defined in
imap_open(). This is not useful for, lets say, getting full threads ( from "Sent Messages"
and "Inbox" [took me a day to figure this out]).
If you compare threads on Outlook vs gmail.com you will find that Outlook determines threads by
subject title, not actual parent > child relationships.
Gmail however, seems to get threads right, but does not include mail you send using their web
interface in {imap.google.com:993/imap/ssl}Sent Messages . What this means is that threads using
php imap won't be perfect for gmail.
If you send mail using Outlook (or any mail client), gmail.com does put it in their "Sent
Mail".
So all in all, threads for PHP imap are not perfect. But I blame the imap specifications (DEAR IMAP
GUYS, please add better uids and parent ids. thx Chris) more than PHP
So I created the Outlook method for threading (comparing subjects) below:
<?php
$imap = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', 'youremail@gmail.com',
'yourpassword');
$subject = 'Item b';
$threads = array();
//remove re: and fwd:
$subject = trim(preg_replace("/Re\:|re\:|RE\:|Fwd\:|fwd\:|FWD\:/i", '',
$subject));
//search for subject in current mailbox
$results = imap_search($imap, 'SUBJECT "'.$subject.'"', SE_UID);
//because results can be false
if(is_array($results)) {
//now get all the emails details that were found
$emails = imap_fetch_overview($imap, implode(',', $results), FT_UID);
//foreach email
foreach ($emails as $email) {
//add to threads
//we date date as the key because later we will sort it
$threads[strtotime($email->date)] = $email;
}
}
//now reopen sent messages
imap_reopen($imap, '{imap.gmail.com:993/imap/ssl}Sent Messages');
//and do the same thing
//search for subject in current mailbox
$results = imap_search($imap, 'SUBJECT "'.$subject.'"', SE_UID);
//because results can be false
if(is_array($results)) {
//now get all the emails details that were found
$emails = imap_fetch_overview($imap, implode(',', $results), FT_UID);
//foreach email
foreach ($emails as $email) {
//add to threads
//we date date as the key because later we will sort it
$threads[strtotime($email->date)] = $email;
}
}
//sort keys so we get threads in chronological order
ksort($threads);
echo '<pre>'.print_r($threads, true).'</pre>';
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/104056
Del: integrated -- https://master.php.net/note/delete/104056/integrated
Del: useless -- https://master.php.net/note/delete/104056/useless
Del: bad code -- https://master.php.net/note/delete/104056/bad+code
Del: spam -- https://master.php.net/note/delete/104056/spam
Del: non-english -- https://master.php.net/note/delete/104056/non-english
Del: in docs -- https://master.php.net/note/delete/104056/in+docs
Del: other reasons-- https://master.php.net/note/delete/104056
Reject -- https://master.php.net/note/reject/104056
Search -- https://master.php.net/manage/user-notes.php