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

[MOD] Staff Reply to Tickets (IMAP)

edited December 2013 in Mods and Customizations
Hi All,

I managed to hack something together that works for me (I'm using IMAP).
I'm sure some of the elements could be streamlined or make better use of existing functions, but it works for me.

Description: Adds the ability for staff to be able to reply via email to tickets.
Works best if the reply separator is added to the top of staff notification emails.

[B]*** Please check later in the thread for updated versions of the mod ***[/B]

Please see below 'diff' listed between the original and my modded files.
If anyone has issues integrating them I'm happy to post some download links to a zip containing the two modded files.

include/class.ticket.php

diff class.ticket.orig class.ticket.php
1544c1544,1552
< $poster = $thisstaff?$thisstaff->getName():'SYSTEM (Canned Reply)';
---
> if(!$vars['name']){
> $poster = $thisstaff?$thisstaff->getName():'SYSTEM (Canned Reply)';
> $msgstaff = $thisstaff?$thisstaff->getId():0;
> $msgip = $thisstaff?$thisstaff->getIP():'';
> } else {
> $poster = $vars['name'];
> $msgstaff = $vars['staffid'];
> $msgip = $_SERVER['REMOTE_ADDR'];
> }
1551c1559
< .' ,staff_id='.db_input($thisstaff?$thisstaff->getId():0)
---
> .' ,staff_id='.db_input($msgstaff)
1553c1561
< .' ,ip_address='.db_input($thisstaff?$thisstaff->getIP():'');
---
> .' ,ip_address='.db_input($msgip);
1952,1953d1959
< if($email)
< $sql.=' AND email='.db_input($email);
1954a1961,1965
> if($email)
> $staffsql = 'SELECT email FROM '.STAFF_TABLE.' WHERE email='.db_input($email);
> if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
> $sql.=' AND email='.db_input($email);
> }
1956c1967
< list($id)=db_fetch_row($res);
---
> list($id)=db_fetch_row($res);



include/class.mailfetch.php

diff class.mailfetch.orig class.mailfetch.php
401a402,403
> $staffsql = 'SELECT staff_id, firstname, lastname, email FROM '.STAFF_TABLE.' WHERE email='.db_input($vars['email']).' LIMIT 1';
> if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
403a406,417
> } else {
> $staffvars=db_fetch_row(db_query($staffsql));
> $replyvars=Array(
> \"msgId\" => $ticket,
> \"response\" => $vars['message'],
> \"name\" => $staffvars['1'].' '.$staffvars['2'],
> \"staffid\" => $staffvars['0'],
> );
> $errors=array();
> if(!($msgid=$ticket->postReply($replyvars, $errors)))
> return false;
> }


If anyone has any suggestions, issues or modifications I would be happy to hear them.
«1345

Comments

  • Could you zip the files?

    Do you think it will work with POP?
  • Also,

    my functions for $poster are different than what your original shows. Are you running the latest version?
  • Thanks for pointing that out. I'm running RC5, so looks like its not the latest.
    I'll update mine and do a compare then make any required changes.
    Will get back to you soon.
  • Working on 1.7 Stable

    Updated from 1.7-RC5 to 1.7 Stable.
    The differences in RC5 and Stable actually simplified the process.
    Should work with both IMAP and POP.

    Please see below 'diff' of same two files as above. I have also attached a zip file containing the files for those who want an easy method of application.

    As above please let me know if there are any issues.

    include/class.ticket.php

    diff class.ticket.orig class.ticket.php
    1676c1676,1679
    < $sql.=' AND email='.db_input($email);
    ---
    > $staffsql = 'SELECT email FROM '.STAFF_TABLE.' WHERE email='.db_input($email);
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > $sql.=' AND email='.db_input($email);
    > }


    include/class.mailfetch.php

    diff class.mailfetch.orig class.mailfetch.php
    406,408c406,421
    < if(!($message=$ticket->postMessage($vars, 'Email')))
    < return false;
    <
    ---
    > $staffsql = 'SELECT staff_id, firstname, lastname, email FROM '.STAFF_TABLE.' WHERE email='.db_input($vars['email']).' LIMIT 1';
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > if(!($message=$ticket->postMessage($vars, 'Email')))
    > return false;
    > } else {
    > $staffvars=db_fetch_row(db_query($staffsql));
    > $replyvars=Array(
    > \"msgId\" => $ticket,
    > \"response\" => $vars['message'],
    > \"poster\" => $staffvars['1'].' '.$staffvars['2'],
    > \"staffId\" => $staffvars['0'],
    > );
    > $errors=array();
    > if(!($msgid=$ticket->postReply($replyvars, $errors)))
    > return false;
    > }
    ReplyPatchv1.zip
    21K
  • I hate to ask a stupid question, but what does this do?
  • It will allow staff to reply by email to a ticket.
  • This worked perfectly.

    Taking it a step further, can you have variables that can change the assignment of a ticket, or even close the ticket?

    Also, when a person responds to the ticket, is there a way to automatically assign the ticket to that staff?
  • I hate to ask a stupid question, but what does this do?
    Thanks chaosratt, have added a description. My original post was in a 1.6 thread and I cut and paste from there and it assumed the context was already know.
    Taking it a step further, can you have variables that can change the assignment of a ticket, or even close the ticket?
    Certainly would be possible. Not something I would necessarily use, but happy to see what I can do for you when I have some idle time (should be two or three lines of code).
    Also, when a person responds to the ticket, is there a way to automatically assign the ticket to that staff?
    This would be possible yes, but would need to be a system wide default along the line of [if owner not set, make this user the owner].
    You would also need to make sure that all staff are receiving notifications of new tickets (and the request is included) which could be quite spammy.
  • Cool, an example of the reply by email commands is as follows with spiceworks

    http://community.spiceworks.com/help/Tickets_Anywhere

    I think the reply to claim would be okay if it is global as well.
  • After adding the mod and verifying it worked, we started getting a DB Error when a customer manually checks the status of the ticket.
    x
    DB Error #1065
    []

    Query was empty
  • Jemson;38760 said:
    Updated from 1.7-RC5 to 1.7 Stable.
    The differences in RC5 and Stable actually simplified the process.
    Should work with both IMAP and POP.

    Please see below 'diff' of same two files as above. I have also attached a zip file containing the files for those who want an easy method of application.

    As above please let me know if there are any issues.

    include/class.ticket.php

    diff class.ticket.orig class.ticket.php
    1676c1676,1679
    < $sql.=' AND email='.db_input($email);
    ---
    > $staffsql = 'SELECT email FROM '.STAFF_TABLE.' WHERE email='.db_input($email);
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > $sql.=' AND email='.db_input($email);
    > }


    include/class.mailfetch.php

    diff class.mailfetch.orig class.mailfetch.php
    406,408c406,421
    < if(!($message=$ticket->postMessage($vars, 'Email')))
    < return false;
    <
    ---
    > $staffsql = 'SELECT staff_id, firstname, lastname, email FROM '.STAFF_TABLE.' WHERE email='.db_input($vars['email']).' LIMIT 1';
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > if(!($message=$ticket->postMessage($vars, 'Email')))
    > return false;
    > } else {
    > $staffvars=db_fetch_row(db_query($staffsql));
    > $replyvars=Array(
    > \"msgId\" => $ticket,
    > \"response\" => $vars['message'],
    > \"poster\" => $staffvars['1'].' '.$staffvars['2'],
    > \"staffId\" => $staffvars['0'],
    > );
    > $errors=array();
    > if(!($msgid=$ticket->postReply($replyvars, $errors)))
    > return false;
    > }
    ok so i downloaded the .zip file and replaced the old files with the new ones. I already had email working were a user could send a email into the help desk and it would send out an alert to staff, however when staff responds to the email even after i uploaded the two files in the .zip file and replaced the old ones it still creates another ticket.

    Any ideas
  • adidasrta;38840 said:
    After adding the mod and verifying it worked, we started getting a DB Error when a customer manually checks the status of the ticket.
    Same thing here.
  • Fixed DB Error

    I found the issue with the DB Error and fixed it.
    Turns out it was the line checking if the email was from a staff member in class.ticket.php

    It is now not necessary to edit class.ticket.php at all (you can return it to original).

    All the modifications are now in class.mailfetch.php as below:


    400c400
    < if(!($ticket=Ticket::lookupByExtId($tid, $vars['email'])))
    ---
    > if(!($ticket=Ticket::lookupByExtId($tid)))
    406,408c406,422
    < if(!($message=$ticket->postMessage($vars, 'Email')))
    < return false;
    <
    ---
    > $staffsql = 'SELECT staff_id, firstname, lastname, email FROM '.STAFF_TABLE.' WHERE email='.db_input($vars['email']).' LIMIT 1';
    > $vars['poster'] = $vars['name'];
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > if(!($message=$ticket->postMessage($vars, 'Email')))
    > return false;
    > } else {
    > $staffvars=db_fetch_row(db_query($staffsql));
    > $replyvars=Array(
    > \"msgId\" => $ticket,
    > \"response\" => $vars['message'],
    > \"poster\" => $staffvars['1'].' '.$staffvars['2'],
    > \"staffId\" => $staffvars['0'],
    > );
    > $errors=array();
    > if(!($msgid=$ticket->postReply($replyvars, $errors)))
    > return false;
    > }


    Zip attached also.
    ReplyPatchv2.zip
    21K
  • miconib;38858 said:
    ok so i downloaded the .zip file and replaced the old files with the new ones. I already had email working were a user could send a email into the help desk and it would send out an alert to staff, however when staff responds to the email even after i uploaded the two files in the .zip file and replaced the old ones it still creates another ticket.

    Any ideas
    Micronib, can you please check what version you are running and advise?
    To do this, go to Admin Panel, Settings, and it should be written across the top of System Preferences screen.
  • Seems like the issue is gone now. Great work.

    The Auto-assign part seems to have an issue (Posted in the other thread).
  • Pipeing

    Ok, i tested this and actually works great, however i'm on a shared host and getting the system to check for email every 5 minutes has not really been working. Is there any way we can do this same thing with email piping. Thanks so much man

    Also my version is osTicket (v1.7.0)
  • miconib;39208 said:
    Ok, i tested this and actually works great, however i'm on a shared host and getting the system to check for email every 5 minutes has not really been working. Is there any way we can do this same thing with email piping. Thanks so much man

    Also my version is osTicket (v1.7.0)
    ? Anyone got any idea's if that would work or is it just not possible. See the thing is, if i send the email from cpanel to the pipe it's instant and will always work. So if i could do the exact same thing as we are doing for the fetch then i would be golden.
  • miconib;39208 said:
    Ok, i tested this and actually works great, however i'm on a shared host and getting the system to check for email every 5 minutes has not really been working. Is there any way we can do this same thing with email piping. Thanks so much man
    There shouldn't be any issues doing the same thing via piping.
    I can't say what if anything would need to be changed in a piping environment as we use Office 365 for our mail (no piping ability) so I am not able to test it for you in my environment I'm afraid.
  • Updated Mod

    Please see below latest update.

    - Added a reply separator to outgoing emails to staff (ticket message updates)
    - Added the strip function on the incoming mail from staff to remove the text below the separator.
    - Included the ability to insert "#close" into email subject to close a ticket.

    include/class.mailfetch.php

    diff class.mailfetch.orig class.mailfetch.php
    400c400
    < if(!($ticket=Ticket::lookupByExtId($tid, $vars['email'])))
    ---
    > if(!($ticket=Ticket::lookupByExtId($tid)))
    406,408c406,423
    < if(!($message=$ticket->postMessage($vars, 'Email')))
    < return false;
    <
    ---
    > $staffsql = 'SELECT staff_id, firstname, lastname, email FROM '.STAFF_TABLE.' WHERE email='.db_input($vars['email']).' LIMIT 1';
    > $vars['poster'] = $vars['name'];
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > if(!($message=$ticket->postMessage($vars, 'Email')))
    > return false;
    > } else {
    > $staffvars=db_fetch_row(db_query($staffsql));
    > $replyvars=Array(
    > \"msgId\" => $ticket,
    > \"response\" => $vars['message'],
    > \"poster\" => $staffvars['1'].' '.$staffvars['2'],
    > \"staffId\" => $staffvars['0'],
    > \"subject\" => $vars['subject'],
    > );
    > $errors=array();
    > if(!($msgid=$ticket->postReply($replyvars, $errors)))
    > return false;
    > }


    include/class.ticket.php

    diff class.ticket.orig class.ticket.php
    1314a1315,1317
    > if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
    > $msg['body'] =\"\n$tag\n\n\".$msg['body'];
    >
    1402a1406,1410
    > //Strip quoted reply...on emailed replies
    > if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()) && strpos($vars['response'], $tag))
    > if(list($msg) = split($tag, $vars['response']))
    > $vars['response'] = $msg;
    >
    1404a1413,1417
    >
    > //Check for actions in the subject
    > if($vars['subject'] && preg_match (\"/#close/\", $vars['subject'], $action)) {
    > return $this->close();
    > }


    Have added modified files zip to this post.
    ReplyPatchv3.zip
    21K
  • Jemson;39252 said:
    Please see below latest update.

    - Added a reply separator to outgoing emails to staff (ticket message updates)
    - Added the strip function on the incoming mail from staff to remove the text below the separator.
    - Included the ability to insert "#close" into email subject to close a ticket.

    include/class.mailfetch.php

    diff class.mailfetch.orig class.mailfetch.php
    400c400
    < if(!($ticket=Ticket::lookupByExtId($tid, $vars['email'])))
    ---
    > if(!($ticket=Ticket::lookupByExtId($tid)))
    406,408c406,423
    < if(!($message=$ticket->postMessage($vars, 'Email')))
    < return false;
    <
    ---
    > $staffsql = 'SELECT staff_id, firstname, lastname, email FROM '.STAFF_TABLE.' WHERE email='.db_input($vars['email']).' LIMIT 1';
    > $vars['poster'] = $vars['name'];
    > if(($staffres=db_query($staffsql)) && (db_num_rows($staffres) == 0)){
    > if(!($message=$ticket->postMessage($vars, 'Email')))
    > return false;
    > } else {
    > $staffvars=db_fetch_row(db_query($staffsql));
    > $replyvars=Array(
    > \"msgId\" => $ticket,
    > \"response\" => $vars['message'],
    > \"poster\" => $staffvars['1'].' '.$staffvars['2'],
    > \"staffId\" => $staffvars['0'],
    > \"subject\" => $vars['subject'],
    > );
    > $errors=array();
    > if(!($msgid=$ticket->postReply($replyvars, $errors)))
    > return false;
    > }


    include/class.ticket.php

    diff class.ticket.orig class.ticket.php
    1314a1315,1317
    > if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
    > $msg['body'] =\"\n$tag\n\n\".$msg['body'];
    >
    1402a1406,1410
    > //Strip quoted reply...on emailed replies
    > if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()) && strpos($vars['response'], $tag))
    > if(list($msg) = split($tag, $vars['response']))
    > $vars['response'] = $msg;
    >
    1404a1413,1417
    >
    > //Check for actions in the subject
    > if($vars['subject'] && preg_match (\"/#close/\", $vars['subject'], $action)) {
    > return $this->close();
    > }


    Have added modified files zip to this post.

    This is amazing, is there any way i can use this with pipe email?
  • Does this work with Exchange email rather than POP/IMAP?
  • I don't see why it wouldn't. Are you still pulling email from IMAP/POP?
  • no, local exchange server... but we do have OWA, so maybe it would still work...?
  • I finally had a chance to test everything out, however I did run into a minor issue.

    The #close option is working and closing the ticket. The issue I am having is that it is not alerting the customer that it is closed, or if there was a message associated with it.

    Any suggestions?
  • adidasrta;39390 said:
    I finally had a chance to test everything out, however I did run into a minor issue.

    The #close option is working and closing the ticket. The issue I am having is that it is not alerting the customer that it is closed, or if there was a message associated with it.

    Any suggestions?
    Does #close simply need to be anywhere within the subject or in the beginning?
    Also, how exactly do you use this?

    Please correct if i'm wrong, the below steps:
    [LIST=1]
    [*]User submits a new ticket
    [*]Department is notified ("New Ticket Alert" via email)
    [*]Any staff member can respond to the New Ticket Alert with #close to close the ticket?
    [*]Any response to that email is sent to the user
    [/LIST]
    Or:
    [LIST=2]
    [*]User submits a new ticket
    [*]Department is notified ("New Ticket Alert" via email)
    [*]The ticket must be assigned to a staff member
    [*]Assigned staff member can respond with #close to close the ticket?
    [*]Any response from that staff via email is sent to the user
    [/LIST]
  • MrJosh;39320 said:
    Does this work with Exchange email rather than POP/IMAP?
    Hi Josh,
    In order to connect osticket to exchange in general you would be using either IMAP or POP on the account. I myself am using Office 365, a hosted exchange platform, but osticket connects to the hosted exchange platform via IMAP.
  • @alltime, both options you mentioned are true. #close just needs to be included anywhere in the subject line.

    @adidasrta, I posted in the other thread, but the issue was the order meaning the reply was not sent:

    See below change required to original ticket.php for reply to be sent with close.


    diff class.ticket.orig class.ticket.php
    1442a1451,1455
    > }
    >
    > //Check for actions in the subject
    > if($vars['subject'] && preg_match (\"/#close/\", $vars['subject'], $action)) {
    > return $this->close();



    Will update the mod with the above soon. Am going to introduce a CC ability soon (time permitting) as well so will upload it all together.
  • For some reason, my instance (1.7.0) just keeps creating new tickets with the files from ReplyPatchv3.zip.

    Do the templates have to have a specific format for the script to recognize it should close or reply? This is an example of a reply as a staff member:

    Subject: Re: New Ticket Alert #close

    Body:

    This issue is now resolved.

    Thank you,
    Support team

    On Mon, May 27, 2013 at 10:50 AM, UHSA IT Support wrote:
    Admin,

    New ticket #815708 created.
    To view/respond to the ticket, click here: http://my.url/scp/tickets.php?id=1935
    -------------------
    Name: user
    Email: email
    Dept: IT Support

    This is just a test.

    Thank you!

    -------------------

    - Your friendly Customer Support System - powered by osTicket.
    What gives?
  • Cron PHP error

    Jemson;

    Thanks for the mod which seems to work but we are now getting the following error emailed from our cron task:

    Subject:

    /usr/bin/php /var/www/vhosts/domain.com/httpdocs/support/api/cron.php

    Body:

    PHP Warning: preg_match() expects parameter 2 to be string, object given in /var/www/vhosts/domain.com/httpdocs/support/include/mysql.php on line 188 PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /var/www/vhosts/domain.com/httpdocs/support/include/mysql.php on line 179

    Can you help?
  • For some reason, my instance (1.7.0) just keeps creating new tickets with the files from ReplyPatchv3.zip.
    Are you running the stable release or one of the RCs?
    The first mod instance was written on RC4 and caused a similar issue on ST. It was then rewritten for ST, so im thinking its possible you are running an RC version and having the same issue?
Sign In or Register to comment.