Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

add reply message to the existing ticket only by checking ticket number in subject

Hello, I have problem, Im using osticket om ny side and different ticket system on other side.

Problem is when Im creating ticket, my message is going to the other ticket system, that ticket system generate new message instead of reply with different message-id. And mine osticket creates new ticket instead of adding this message to the existing ticket.
I need to check only ticket number in subject-id without message-id and references.
Could someone help me?
I think the main function in class.thread.php file


  • 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['mid'] &&
                    ($entry = ThreadEntry::objects()
                     ->filter(array('email_info__mid' => $mailinfo['mid']))
             ) {
                $seen = true;
                return $entry;

            $possibles = array();
            foreach (array('mid', 'in-reply-to', 'references') as $header) {
                $matches = array();
                if (!isset($mailinfo[$header]) || !$mailinfo[$header])
                // Header may have multiple entries (usually separated by
                // spaces ( )
                elseif (!preg_match_all('/<([^>@]+@[^>]+)>/', $mailinfo[$header],

                // The References header will have the most recent message-id
                // (parent) on the far right.
                // @see rfc 1036, section 2.2.5
                $possibles = array_merge($possibles, array_reverse($matches[1]));

            // Add the message id if it is embedded in the body
            $match = array();
            if (preg_match('`(?:class="mid-|Ref-Mid: )([^"\s]*)(?:$|")`',
                    (string) $mailinfo['message'], $match)
                && !in_array($match[1], $possibles)
            ) {
                $possibles[] = $match[1];

            $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::decodeMessageId($mid);
                if (!$mid_info || !$mid_info['loopback'])
                if (isset($mid_info['uid'])
                    && @$mid_info['entryId']
                    && ($t = ThreadEntry::lookup($mid_info['entryId']))
                    && ($t->thread_id == $mid_info['threadId'])
                ) {
                    if (@$mid_info['userId']) {
                        $mailinfo['userId'] = $mid_info['userId'];
                    elseif (@$mid_info['staffId']) {
                        $mailinfo['staffId'] = $mid_info['staffId'];

                    // Capture the user type
                    if (@$mid_info['userClass'])
                        $mailinfo['userClass'] = $mid_info['userClass'];

                    // ThreadEntry was positively identified
                    return $t;
            // Passive threading - listen mode
            if (count($possibles)
                    && ($entry = ThreadEntry::objects()
                        ->filter(array('email_info__mid__in' => array_map(
                            function ($a) { return "<$a>"; },
             ) {
                $mailinfo['passive'] = true;
                return $entry;
  •        // Search for ticket by the [#123456] in the subject line
            // This is the last resort -  emails must match to avoid message
            // injection by third-party.
            $subject = $mailinfo['subject'];
            $match = array();
            if ($subject
                    && $mailinfo['email']
                    // Required `#` followed by one or more of
                    //      punctuation (-) then letters, numbers, and symbols
                    // (Try not to match closing punctuation (`]`) in [#12345])
                    && preg_match("/#((\p{P}*[^\p{C}\p{Z}\p{P}]+)+)/u", $subject, $match)
                    //Lookup by ticket number
                    && ($ticket = Ticket::lookupByNumber($match[1]))
                    //Lookup the user using the email address
                    && ($user = User::lookup(array('emails__address' => $mailinfo['email'])))) {
                //We have a valid ticket and user
                if ($ticket->getUserId() == $user->getId() //owner
                        ||  ($c = Collaborator::lookup( // check if collaborator
                                array('user_id' => $user->getId(),
                                      'thread_id' => $ticket->getThreadId())))) {

                    $mailinfo['userId'] = $user->getId();
                    return $ticket->getLastMessage();

            return null;

  • This does not appear to be a suggestion or feedback.

    osTicket will try to match using multiple ways, and if it fails match on the ticket # in the subject.  That being said there would not be a ticket number that matched in a second ticket system.  Well unless you were doing some sort of real time replication or got really unlucky using random assignment.

    There is no way to do this using stock osTicket UI.
  • thanks for the reply.
    Subject stays the same:   RE: Ticket [#000021] 
    but message-id is different. And osticket creates new ticket instead of adding this message to the existing ticket.
  • edited April 2017
    I can confirm that OSTickets 1.10 does not match on the ticket-number in the subject - we got a lot of new tickets with "#1234 in the subject of the customers mail!

    we had the same Problem in the past in ostickets 1.9x - the reason was, that ostickets didnt assign the message was not sent from an address which was stored as collaborator of the ticket........ so i changed the code in class.thread.php at below line 940 (accepted the mail and added the sender as collaborator..):

    // We found a match  - see if we can ID the user.
    // XXX: Check access of ref is enough?
     /* Comfine MOD Start */
                        /* we dont want the check of the user ID ... Check of REF-ID is enough!
                        if ($ref && ($uid = $t->getUIDFromEmailReference($ref))) {
                            if ($ref[0] =='s') //staff
                                $mailinfo['staffId'] = $uid;
                            else // user or collaborator.
                                $mailinfo['userId'] = $uid;

                            // Best possible case — found the thread and the
                            // user
                            $user = User::lookupByEmail($mailinfo['email']);
                            // if user is not already in system, create him              
                                   return null;
                            return $t;
                        /* Comfine MOD End */

    and it worked well, but now in 1.10 the entire code is very different and i cannot find the right location to modify the code, ntozier, can you please give me a hint where we can change this behavier in the 1.10? ...i stuck at class.mailer.php in the decodeMessageId method and i don't understand the code.
    thanks in advance!
  • <-- forum moderator.  Not a project dev.  I have no idea where you would change something like this in the code.
  • Thanks for the MOD Comfine. 
    ntozier could you ask dev to help us?
  • Hello Arly.

    I have this problem too...

    Did you already found out how to change this in version 1.10?

    We would love to have this working in our setup as well.

    Thank you 
Sign In or Register to comment.