Managing additional Email recipients

In this example you want to create a custom Email Rule to store all the recipients of an email that generates a new operation, and then eventually add them in Cc to the notifications generated during its life cycle.

Two new custom fields will be created in the operations, one to manage recipients who are also registered users in Deepser, the other to store only email addresses.

STEP1: Create custom field for registered users in Deepser

Create a custom field in the DeepService – Operation model with the following characteristics:

  • Column Name: cust_operation_cc_registered
  • Label: Additional Recipients
  • Column Type: Don’t create DB clumn
  • Element Type: Userassociation

STEP2: Create custom field for unregistered users in Deepser

Create a custom field in the DeepService – Operation model with the following characteristics:

  • Column Name: cust_operation_cc_unregistered
  • Label: Additional Recipients (Not Registered)
  • Column Type: Text area
  • Element Type: Textarea

STEP3: Email Rule Creation

 Create a new Email Rule, set name, priority (higher than a possible default rule) and status ‘Enabled’.

Set, in the ‘Apply On’ field the value ‘On Create‘ to indicate that the Email Rule should be executed if the message does not belong to an existing thread.

STEP4: Email Rule Configuration

In the “Set Main Model Values” Query Builder, Set the following values:

Insert in the scripting area of the ‘Set Main Model Values‘ field the following PHP code, which implements the logic for saving, on the operation custom fields, any additional recipients.

 * Additional Recipient Managment 
//retreive already parsed Tos
$to = Deep::helper('deep_util')->parseEmailString($this->getMessage()->getTo());

//retreive already parsed Ccs
$cc = Deep::helper('deep_util')->parseEmailString($this->getMessage()->getCc());

if((!is_null($cc) && is_array($cc) && !is_null($to) && is_array($to) )) {
    //merge to and cc arrays, and change keys case
    $recipients = array_change_key_case(array_merge($to,$cc), CASE_LOWER);
    //exclude incoming mailboxes defined in deepser to avoid loops
    $mailboxCollection = Deep::getResourceModel('deep_email/mailbox_collection')
        ->addFieldToFilter('type',['eq' => 1]);
    foreach($mailboxCollection as $mailbox){
    //manual exclusion other emailboxes that forward emails to deepser to avoid loops
    $ccUnregistered = [];
    $ccRegistered = [];
    //for each additional recipient determine if it is a registered user or not
    foreach($recipients as $recipient => $name){
        $user = Deep::getModel('deep_admin/user')->loadByEmail($recipient);
        if($user && $user->getId()){
            //populate userassoc array
            $ccRegistered [] = $user->getUsername();
            //populate unregistered user array
            $ccUnregistered [] = $recipient;
        //populate the userassociation, that is the cust_operation_cc_registered field
        $this->getModel()->addUserAssociations('watch', $ccRegistered);
    if(count($ccUnregistered)) {
        //populate the custom field cust_operation_cc_unregistered, adding email addresses separated by ;
        $this->getModel()->setData('cust_operation_cc_unregistered', implode(';', $ccUnregistered));
Note: If the user association is populated by a rule that does not make any changes to the ticket or in general to the entity to which the user association belongs, the user association will not be modified.