note 125377 added to function.imap-thread
| From: | p2.eduardoruiz.es via php-notes | Date: | Sat, 19 Sep 2020 07:20:08 +0000 |
| Subject: | note 125377 added to function.imap-thread | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215140@lists.php.net to get a copy of this message | ||
Get all UIDs of a single thread (not the whole all messages threads).
If you want to list just a conversation thread.
First, we must find the root element, using an UID included in these conversation in whatever
message.
Then recursivelly we list all UIDs messages in an array.
<?php
// Function returns empty array if message is not in a thread
// You must indicate $mailbox returned by imap_open and a message UID listed in that thread
conversation
// First root element is found when parent and previous parent are both with 'XX.next'
elements equal to 0
function FindThread($mailbox, $uid) {
$mails = array();
$thread = imap_thread($mailbox, SE_UID);
$i = array_search($uid, $thread);
while(($tree = explode('.', $i)) && ($j = array_search($tree[0], $thread))
&&
($treej = explode('.', $j)) && $treej[0] && ($k =
array_search($treej[0], $thread)) && ($treek = explode('.', $k)) &&
($thread[$treej[0] . '.next'] != 0 || $thread[$treek[0] . '.next'] != 0))
$i = $j;
$mails = FindThreadAll($thread, $tree[0]);
return $mails;
}
// Walk messages in 2 ways, using 'XX.branch' and recursivelly 'XX.next' for
sibiling and children messages
// Stop when in first iteration(call) there is no 'XX.next' element
function FindThreadAll(&$thread, $i, $n = 0) {
$mails = array();
while($i) {
if(!($j = $thread[$i . '.next']) && !$n)
break;
$mails[] = $thread[$i . '.num'];
if($j)
$mails = array_merge($mails, FindThreadAll($thread, $j, $n + 1));
$i = $thread[$i . '.branch'];
}
return $mails;
}
if(($mails = FindThread(64851))) // Example UID message
print_r(imap_fetch_overview($mailbox, implode(',', $mails), SE_UID));
?>
Eduardo Ruiz (Spain)
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.172 (proxied: 85.31.132.140)
----
Manual Page -- http://php.net/manual/en/function.imap-thread.php
Edit -- https://master.php.net/note/edit/125377
Del: integrated -- https://master.php.net/note/delete/125377/integrated
Del: useless -- https://master.php.net/note/delete/125377/useless
Del: bad code -- https://master.php.net/note/delete/125377/bad+code
Del: spam -- https://master.php.net/note/delete/125377/spam
Del: non-english -- https://master.php.net/note/delete/125377/non-english
Del: in docs -- https://master.php.net/note/delete/125377/in+docs
Del: other reasons-- https://master.php.net/note/delete/125377
Reject -- https://master.php.net/note/reject/125377
Search -- https://master.php.net/manage/user-notes.php