- Edited
This works for me, yes it is very hackish. Could somebody verify it works for them?
This will allow staff to reply to any email from osTicket and expect it to be added to the ticket as a response. It only works if the first part of the email address is the same as the staff usernames.
In pipe.php
Use the first and last line for reference.
be sure to read the comments, you have to change one part for your setup.
//Allow mismatched emails?? For now hell NO.
//if(!is_object($ticket) || strcasecmp($ticket->getEmail(),$var))
// $ticket=null;
}
$errors=array();
$msgid=0;
if(!$ticket){ //New tickets...
$ticket=Ticket:($var,$errors,'email');
if(!is_object($ticket) || $errors){
api_exit(EX_DATAERR,'Create Failed '.implode("\n",$errors)."\n\n");
}
$msgid=$ticket->getLastMsgId();
}else{
$message=$var;
//Strip quoted reply...TODO: figure out how mail clients do it without special tag..
if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()) && strpos($var,$tag))
list($message)=split($tag,$var);
//post message....postMessage does the cleanup.
$senderName=$var;
$senderName = substr($senderName, 0, -20); //Change this to remove the <USERMENTION username="youyrdomain.com">@youyrdomain.com</USERMENTION> from the email address
$sql1="SELECT staff_id FROM ost_staff WHERE username='$senderName'";
$almostsenderid=db_query($sql1);
while($row = mysql_fetch_array($almostsenderid)) {
$senderid = $row;
}$sql="SELECT firstname,lastname FROM ost_staff WHERE staff_id='$senderid'";
$almostsendername=db_query($sql);
while($row = mysql_fetch_array($almostsendername)) {
$senderName = $row.' '.$row;
}if ($senderid){
if(!($respId=$ticket->postEmailResponse($msgid,$senderid,$senderName,$message,$var,'Email'))) {
api_exit(EX_DATAERR,"post message failed \n\n $message\n");
}
}else{
if(!($msgid=$ticket->postMessage($message,$var,'Email'))) {
api_exit(EX_DATAERR,"post message failed \n\n $message\n");
}
}}//Ticket created...save attachments if enabled.
Add this to class.ticket.php:
You can add it anywhere within the current functions
function postEmailResponse($mymsgid,$senderid,$senderName,$msg,$signature='none',$attachment=false,$ticket_status,$canalert=true){
global $cfg;
if(!$this->getId())
return 0;
//We don't really care much about the source at message level
$source=$source?$source:$_SERVER;
$mytid=db_input($this->getId());
$sql5="SELECT msg_id FROM ost_ticket_message WHERE ticket_id='$mytid' order by msg_id desc limit 1";
$result5=db_query($sql5);
while($row = mysql_fetch_array($result5)) {
$mymsgid = $row;
}
$sql= 'INSERT INTO '.TICKET_RESPONSE_TABLE.' SET created=NOW() '.
',msg_id='.db_input($mymsgid).
',ticket_id='.db_input($this->getId()).
',response='.db_input(Format:($msg)). //Tags/code stripped...meaning client can not send in code..etc
',staff_id='.db_input($senderid).
',staff_name='.db_input($senderName).
',ip_address='.db_input('10.2.1.22');
$resp_id=0;
//echo $sql;
$sql1= 'UPDATE '.TICKET_TABLE.' SET updated=NOW() WHERE ticket_id='.db_input($this->getId());
db_query($sql1);
if(db_query($sql) && ($resp_id=db_insert_id())):
if(!$canalert) //No alert/response
return $resp_id;
$dept=$this->getDept();
//Send Response to client...based on the template...
//TODO: check department level templates...if set.
$sql='SELECT ticket_reply_subj,ticket_reply_body FROM '.EMAIL_TEMPLATE_TABLE.
' WHERE cfg_id='.db_input($cfg->getId()).' AND tpl_id='.db_input($cfg->getDefaultTemplateId());
$resp=db_query($sql);
if(db_num_rows($resp) && list($subj,$body)=db_fetch_row($resp)){
if(strtolower($ticket_status)=="close"||strtolower($ticket_status)=="closed") $subj = " - Closed - Re: %subject";
$subj = str_replace("%ticket", $this->getExtId(),$subj);
$subj = str_replace("%subject", $this->getSubject(),$subj);
$body = str_replace("%ticket", $this->getExtId(),$body);
$body = str_replace("%name", $this->getName(),$body);
$body = str_replace("%email", $this->getEmail(),$body);
$body = str_replace("%url", $cfg->getBaseUrl(),$body);
$body = str_replace("%message",$msg,$body);
//Figure out the signature to use...if any.
switch(strtolower($signature)):
case 'mine';
$signature=$thisuser->getSignature();
break;
case 'dept':
$signature=$dept->isPublic()?$dept->getSignature():''; //make sure it is public
break;
case 'none';
default:
$signature='';
break;
endswitch;
$body = str_replace("%signature",$signature,$body);
//Email attachment when attached AND if emailed.
if(($attachment && is_file($attachment)) && $cfg->emailAttachments()) {
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$body = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n".
$body . "\n\n";
$body.= "--{$mime_boundary}\n" .
"Content-Type: " . $attachment . ";\n" .
" name=\"" . $attachment . "\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"" . $attachment . "\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
chunk_split(base64_encode(file_get_contents($attachment))). "\n\n" .
"--{$mime_boundary}--\n";
}
$email=$from=$fromNamenull;
if(($email=$dept->getEmail())) { //Dept email if set!
$from=$email->getEmail();
$fromName=$email->getName();
//Reply separator tag.
if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
$body ="\n$tag\n\n".$body;
}else{//No emails means it is a noreply...
$from=$cfg->getNoReplyEmail();
}
Misc:($this->getEmail(),$subj,$body,$from,$fromName,$headers);
}else{
//We have a big problem...alert admin...
$msg='Problems fetching response template for ticket#'.$this->getId().' Possible config error';
Misc:('System Error',$msg);
}
return $resp_id;
endif;
return 0;
}