Page 1 of 1

Piping multiple emails in Cc and To into Followers

Posted: Sat Nov 30, 2024 10:42 am
by Cegri
Version of script: 3.5.1

Here is a small contribution, similar to this thread. That one was adding emails into the same field with comas. This one adds them as individual Followers.

Acknowledgment: @adrfazenda's solution in this message helped a lot.
Disclaimer: I'm not a programmer. I copy and tweak others' codes. And I'm new to Hdesk!

The aim is to add all extra adresses in the CC and TO fields of email to the Followers table. For a new ticket, it will add all the mails. For a reply, it checks existing followers.
For a new ticket, the $tmpvar['] is modifed to include ['follower_ids']. The existing hesk_newTicket function writes to the dBase. For a reply, new followers are written directly to dBase.

Code;
Add the following two funtions to the bottom of inc\pipe_functions.inc.php

Code: Select all

function hesk_contributed_getFollowersFromEmail($results, $owner, $webmaster_mail)
{
	// Get emails from "Cc:" and "To:"
	foreach ($results['cc'] as $k => $v)
	{
		$tmp[] = $v['address'];
	}
	foreach ($results['to'] as $k => $v)
	{
		$tmp[] = $v['address'];
	}
	
	// Validate emails, get or create customer IDs (skip ticket owner and support email)
	foreach ($tmp as $k => $v)
	{
		$tmpAdress = hesk_validateEmail($v,'ERR',0);
		if ($tmpAdress)
		{
			if ( $tmpAdress != $webmaster_mail &&  $tmpAdress != $owner )
			{
				$tmpFollowers[] = hesk_get_or_create_follower($tmpAdress);
			}
		}
	}
	
	return $tmpFollowers;
}

function hesk_contributed_addFollowers($followers, $ticketID) 
{
	global $hesk_settings;

	// Get current followers of the ticket from dbase
	$res = hesk_dbQuery("SELECT * FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."ticket_to_customer` WHERE `ticket_id`='".$ticketID."'");
	
	while ($row=hesk_dbFetchAssoc($res))
	{
		$currentFollowers[]= $row['customer_id'];
	}
	
	// Add new followers
	foreach($followers as $k => $v)
	{
		if( !in_array($v, $currentFollowers) ) //If not already in list
		{
			hesk_dbQuery("INSERT INTO `".hesk_dbEscape($hesk_settings['db_pfix'])."ticket_to_customer` (`ticket_id`, `customer_id`, `customer_type`)
    	    		VALUES (".intval($ticketID).", ".intval($v).", 'FOLLOWER')");
		}
	}
	
	return;
}
We need to add the following two sections into the hesk_email2ticket function :
(Somewhere around line 270)

Code: Select all

	// Delete the temporary files
	deleteAll($results['tempdir']);
// BEGIN ADDED CODE
	//Get all additional adresses in CC and To fields to add as Followers
	$tmpvar['follower_ids'] =  hesk_contributed_getFollowersFromEmail( $results, $tmpvar['email'], $hesk_settings['webmaster_mail'] );
// END ADDED CODE
	// If this is a reply add a new reply
	if ($is_reply)
	{
		// Set last replier name to customer name
		$ticket['lastreplier'] = ($tmpvar['name'] == $hesklang['pde']) ? $tmpvar['email'] : $tmpvar['name'];;
        $ticket['customer_id'] = hesk_get_or_create_customer($tmpvar['name'], $tmpvar['email']);

		// If staff hasn't replied yet, keep ticket status "New", otherwise set it to "Waiting reply from staff"
		$ticket['status'] = $ticket['status'] ? 1 : 0;
// BEGIN ADDED CODE
		// Add any new followers
		if($tmpvar['follower_ids'])
		{
			hesk_contributed_addFollowers( $tmpvar['follower_ids'], intval($ticket['id']) );
		}
// END ADDED CODE
		// Update ticket as necessary
		hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` SET `lastchange`=NOW(),`status`='{$ticket['status']}',`replies`=`replies`+1,`lastreplier`='0' WHERE `id`='".intval($ticket['id'])."'");
Also we need to delete or disable the following line around line 415:

Code: Select all


	// Insert ticket to database
    //$tmpvar['follower_ids'] = []; // This line needs to be disabled or deleted of the ids not to be deleted

    // Create ticket
	$ticket = hesk_newTicket($tmpvar);

Re: Piping multiple emails in Cc and To into Followers

Posted: Mon Dec 02, 2024 8:35 pm
by Klemen
Thanks for sharing your modifications. I didn't get the chance to test them yet, but something like this will be implemented in the official version, too, in the future.