I think i need to change something here function lookupByEmailHeaders(&$mailinfo, &$seen=false) { // Search for messages using the References header, then the // in-reply-to header if ($mailinfo && ($entry = ThreadEntry:() ->filter(array('email_info__mid' => $mailinfo)) ->order_by(false) ->first() ) ) { $seen = true; return $entry; } $possibles = array(); foreach (array('mid', 'in-reply-to', 'references') as $header) { $matches = array(); if (!isset($mailinfo) || !$mailinfo) continue; // Header may have multiple entries (usually separated by // spaces ( ) elseif (!preg_match_all('/<(+@+)>/', $mailinfo, $matches)) continue; // The References header will have the most recent message-id // (parent) on the far right. // @[deleted] rfc 1036, section 2.2.5 // @[deleted] http://www.jwz.org/doc/threading.html $possibles = array_merge($possibles, array_reverse($matches)); } // Add the message id if it is embedded in the body $match = array(); if (preg_match('`(?="mid-|Ref-Mid: )(*)(?:$|")`', (string) $mailinfo, $match) && !in_array($match, $possibles) ) { $possibles = $match; } $thread = null; foreach ($possibles as $mid) { // Attempt to detect the ticket and user ids from the // message-id header. If the message originated from // osTicket, the Mailer class can break it apart. If it came // from this help desk, the 'loopback' property will be set // to true. $mid_info = Mailer:($mid); if (!$mid_info || !$mid_info) continue; if (isset($mid_info) && @$mid_info && ($t = ThreadEntry:($mid_info)) && ($t->thread_id == $mid_info) ) { if (@$mid_info) { $mailinfo = $mid_info; } elseif (@$mid_info) { $mailinfo = $mid_info; } // Capture the user type if (@$mid_info) $mailinfo = $mid_info; // ThreadEntry was positively identified return $t; } } // Passive threading - listen mode if (count($possibles) && ($entry = ThreadEntry:() ->filter(array('email_info__mid__in' => array_map( function ($a) { return "<$a>"; }, $possibles))) ->first() ) ) { $mailinfo = true; return $entry; }