KevinTheJedi
I was able to get this mostly working in this function but I have one issue I'm a little stumped on. When an external email address such as my gmail sends in a ticket, notifications are sent to collaborators correctly. For some reason when someone who is in the ticketing system already as an agent submits a ticket via email, no notification gets sent. We are using the same CC collaborators for all tests. It looks like the function gets called no matter what to me, and I put my custom code at the top of the function before any returns. Code is below. I am stumped so any help is appreciated. Thanks!
function onNewTicket($message, $autorespond=true, $alertstaff=true) {
global $cfg;
/* ------ SEND OUT NEW TICKET AUTORESP && ALERTS ----------*/
//***********************************************************************************************
// Custom code, sends an email to cc collaboraters when the ticket is created
//Message Headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: system@domain.com" . "\r\n";
// Emails come out in a way thet needs formatting
// Convert everything into an array and strip out spaces
// Will filter by emails only later
$to = print_r($this->last_message->_headers[Cc],true);
$to = str_replace("<","",$to);
$to = str_replace(">","",$to);
$to = str_replace(",","",$to);
// $to is an array now
$to = explode(" ", $to);
//Get Long ticket number
$ticketNumber = print_r($this->ht[number],true);
// Get ticket ID
$ticketId = print_r($this->ht[ticket_id],true);
$body = $ticketNumber;
// Get subject
$ticketSubj = print_r($this->last_message->_headers[Subject],true);
//create body of message
$body = "<html>
<body>
<p>
<strong>Hello,</strong></br></br>
Our Customer Care Team has added you as a collaborator on a ticket,
<a href=http://192.168.1.125/tickets.php?id=" . $ticketId . "> #" . $ticketNumber . "</a>
with the following subject: </br></br>
Subject: <strong>" . $ticketSubj . "</strong>
</p>
</body>
</html>";
// This is so the notification only get sent to users with a Email address
$ceatDomains = array("domain.io", "domain.com", "domain2.com");
// Send the email, Fitlering to make sure there is an @ sign in string
foreach ($to as $toOut)
{
if(strpos($toOut, "@") !== false)
{
// Filter to make sure email is sent only to Employees
foreach ($ceatDomains as $domainName)
{
if (stripos($toOut, $domainName) !== false)
{
mail($toOut, "Collaborator Notification", $body, $headers);
}
}
}
}
//***********************************************************************************************
//Log stuff here...
if (!$autorespond && !$alertstaff)
return true; //No alerts to send.
if(!$cfg
|| !($dept=$this->getDept())
|| !($tpl = $dept->getTemplate())
|| !($email=$dept->getAutoRespEmail())
) {
return false; //bail out...missing stuff.
}
$options = array();
if (($message instanceof ThreadEntry)
&& $message->getEmailMessageId()) {
$options += array(
'inreplyto'=>$message->getEmailMessageId(),
'references'=>$message->getEmailReferences(),
'thread'=>$message
);
}
else {
$options += array(
'thread' => $this->getThread(),
);
}
//Send auto response - if enabled.
if ($autorespond
&& $cfg->autoRespONNewTicket()
&& $dept->autoRespONNewTicket()
&& ($msg = $tpl->getAutoRespMsgTemplate())
)
{
$msg = $this->replaceVars(
$msg->asArray(),
array('message' => $message,
'recipient' => $this->getOwner(),
'signature' => ($dept && $dept->isPublic())?$dept->getSignature():''
)
);
$email->sendAutoReply($this->getOwner(), $msg['subj'], $msg['body'], null, $options);
}
// Send alert to out sleepy & idle staff.
if ($alertstaff
&& $cfg->alertONNewTicket()
&& ($email=$dept->getAlertEmail())
&& ($msg=$tpl->getNewTicketAlertMsgTemplate())
) {
$msg = $this->replaceVars($msg->asArray(), array('message' => $message));
$recipients = $sentlist = array();
// Exclude the auto responding email just incase it's from staff member.
if ($message instanceof ThreadEntry && $message->isAutoReply())
$sentlist[] = $this->getEmail();
if ($dept->getNumMembersForAlerts()) {
// Only alerts dept members if the ticket is NOT assigned.
$manager = $dept->getManager();
if ($cfg->alertDeptMembersONNewTicket() && !$this->isAssigned()
&& ($members = $dept->getMembersForAlerts())
) {
foreach ($members as $M)
if ($M != $manager)
$recipients[] = $M;
}
if ($cfg->alertDeptManagerONNewTicket() && $manager) {
$recipients[] = $manager;
}
// Account manager
if ($cfg->alertAcctManagerONNewTicket()
&& ($org = $this->getOwner()->getOrganization())
&& ($acct_manager = $org->getAccountManager())
) {
if ($acct_manager instanceof Team)
$recipients = array_merge($recipients, $acct_manager->getMembers());
else
$recipients[] = $acct_manager;
}
foreach ($recipients as $k=>$staff) {
if (!is_object($staff)
|| !$staff->isAvailable()
|| in_array($staff->getEmail(), $sentlist)
) {
continue;
}
$alert = $this->replaceVars($msg, array('recipient' => $staff));
$email->sendAlert($staff, $alert['subj'], $alert['body'], null, $options);
$sentlist[] = $staff->getEmail();
}
}
// Alert admin ONLY if not already a staff??
if ($cfg->alertAdminONNewTicket()
&& !in_array($cfg->getAdminEmail(), $sentlist)
&& ($dept->isGroupMembershipEnabled() != Dept::ALERTS_DISABLED)) {
$options += array('utype'=>'A');
$alert = $this->replaceVars($msg, array('recipient' => 'Admin'));
$email->sendAlert($cfg->getAdminEmail(), $alert['subj'],
$alert['body'], null, $options);
}
}
return true;
}