I am trying to write a customization so that when a customer emails in with people CC'd to the email, the people who are CC'd get a Ticket Opened Email as well.

I have been doing some digging myself through class.ticket.php and class.mailfetch.php and tried putting some test code in a couple places then submitting an email to see if my test code gets executed. So far it seems to not be working.

I thought onMessage in class.ticket.php looked promising but I couldn't get it to send an email out to the people who are CC'd.

Any ideas on where to start? Thanks!

@sstephens

Look at the onNewTicket() method. This is what send the New Ticket Alert to the Ticket Owner, and Agents. So get all collalbs in the method itself or pass collabs when the method is called and foreach them.

Cheers.

    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;
    }

      @sstephens

      Since this is custom code, you'll have to do some debugging on your own.

      P.S.
      I don't know why you are "reinventing the wheel" by generating an email like that; we already have methods to send emails/alerts...you don't need to set header, etc. Look at examples like:

      Cheers.

      10 months later

      Hey sstephens we're also looking to set up this mod, so I was wondering if in the end you mananged to solve your issue and if you could share you solution 🙂

      Cheers!
      Julien

      7 days later

      Hey
      For anyone who's interested, I've made the mods described here: https://forum.osticket.com/d/88896-notify-collaborators-also-on-new-ticket/3

      And it does the job!

      The code isn't easy to read, so I'm pasting it here in a more formatted way:

       //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);
      
                  // Notify collaborators on New Ticket - see https://forum.osticket.com/d/88896-notify-collaborators-also-on-new-ticket
                  // START WORKAROUND
                  if ($cfg->notifyCollabsONNewMessage()) {
                      // Check if Collaborators should be notified.
                      $this->notifyCollaborators($message,
                      array('signature' => ($dept && $dept->isPublic())?$dept->getSignature():''));
                  }          
                  // You have to check the Auto-responder setting for new ticket to ticket owner.
                  // END WORKAROUND  
              }
      Write a Reply...