Case Closure - Only Email to One Contact

Hello, in SuiteCRM 7.10.11 when you close a case the Email Notification is only sent to one contact, if the case has many contacts, only one of them is notified.

I’m not an expert developer but I’ve managed to make it work but it needs some touches, professionals ones :lol:

This is the original code

private function sendClosureEmail(aCase $case)
    {
        if (!isAOPEnabled()) {
            return true;
        }
        $GLOBALS['log']->warn('CaseUpdatesHook: sendClosureEmail called');
        require_once 'include/SugarPHPMailer.php';
        $mailer = new SugarPHPMailer();
        $admin = new Administration();
        $admin->retrieveSettings();
        $mailer->prepForOutbound();
        $mailer->setMailerForSystem();
        $emailTemplate = new EmailTemplate();
        $aop_config = $this->getAOPConfig();
        $emailTemplate->retrieve($aop_config['case_closure_email_template_id']);
        if (!$emailTemplate->id) {
            $GLOBALS['log']->warn('CaseUpdatesHook: sendClosureEmail template is empty');
            return false;
        }
        $contact = $case->get_linked_beans('contacts', 'Contact');
        if ($contact) {
            $contact = $contact[0];
        } else {
            return false;
        }
        $emailSettings = getPortalEmailSettings();
        $text = $this->populateTemplate($emailTemplate, $case, $contact);
        $mailer->Subject = $text['subject'];
        $mailer->Body = $text['body'];
        $mailer->isHTML(true);
        $mailer->AltBody = $text['body_alt'];
        $mailer->From = $emailSettings['from_address'];
        isValidEmailAddress($mailer->From);
        $mailer->FromName = $emailSettings['from_name'];
        $email = $contact->emailAddress->getPrimaryAddress($contact);
        $mailer->addAddress($email);
        try {
            if ($mailer->send()) {
                $this->logEmail($email, $mailer, $case->id);
                return true;
            }
        } catch (phpmailerException $exception) {
            $GLOBALS['log']->fatal('CaseUpdatesHook: sending email Failed:  ' . $exception->getMessage());
        }
        $GLOBALS['log']->info('CaseUpdatesHook: Could not send email:  ' . $mailer->ErrorInfo);
        return false;
    }

This is what I’ve changed, with comments about how to and what needs to be reviewed

<?php
private function sendClosureEmail(aCase $case)
    {
        if (!isAOPEnabled()) {
            return true;
        }
        $GLOBALS['log']->warn('CaseUpdatesHook: sendClosureEmail called');
        require_once 'include/SugarPHPMailer.php';
        $mailer = new SugarPHPMailer();
        $admin = new Administration();
        $admin->retrieveSettings();
        $mailer->prepForOutbound();
        $mailer->setMailerForSystem();
        $emailTemplate = new EmailTemplate();
        $aop_config = $this->getAOPConfig();
        $emailTemplate->retrieve($aop_config['case_closure_email_template_id']);
        if (!$emailTemplate->id) {
            $GLOBALS['log']->warn('CaseUpdatesHook: sendClosureEmail template is empty');
            return false;
        }
        $contact = $case->get_linked_beans('contacts', 'Contact');
        /*if ($contact) {
            $contact = $contact[0];
        } else {
            return false;
        }*/
		/*The above code avoids this PHP Notice:  Trying to get property of non-object in /var/www/html/modules/AOP_Case_Updates/CaseUpdatesHook.php but I can't find a way to make it work with what I've changed*/
        $emailSettings = getPortalEmailSettings();
        $text = $this->populateTemplate($emailTemplate, $case, $contact);
        $mailer->Subject = $text['subject'];
        $mailer->Body = $text['body'];
        $mailer->isHTML(true);
        $mailer->AltBody = $text['body_alt'];
        $mailer->From = $emailSettings['from_address'];
        isValidEmailAddress($mailer->From);
        $mailer->FromName = $emailSettings['from_name'];
        /*$email = $contact->emailAddress->getPrimaryAddress($contact);
        $mailer->addAddress($email);*/
		/*These two lines they send just one mail*/
		/*add*/
		foreach($contact as $contacts)
                {
                        $email = $contacts->emailAddress->getPrimaryAddress($contacts);
                        $mailer->AddAddress($email);
                        $string .= $email.','; //needs to be improved "PHP Notice:  Undefined variable: string in /var/www/html/modules/AOP_Case_Updates/CaseUpdatesHook.php"
                }
		/*add*/
        try {
            if ($mailer->send()) {
                //$this->logEmail($email, $mailer, $case->id);
				/*changed*/
				$this->logEmail($string, $mailer, $case->id);
				/*changed*/
				/*the above code writes only one email in suitecrm email module with many destinations, this is just a simple way instead of having one email created in the email module for each notifed contact, usually when we close a case all contacts are added*/
                return true;
            }
        } catch (phpmailerException $exception) {
            $GLOBALS['log']->fatal('CaseUpdatesHook: sending email Failed:  ' . $exception->getMessage());
        }
        $GLOBALS['log']->info('CaseUpdatesHook: Could not send email:  ' . $mailer->ErrorInfo);
        return false;
    }
	?>

I hope that some developer grabs this and makes it the way it should be. The next thing I’m focusing on is the never fixed issue regarding attachements in case updates. https://github.com/salesagility/SuiteCRM/issues/2174 2 years ago :ohmy:

I’ve found this bug reported here https://github.com/salesagility/SuiteCRM/issues/747 3 years ago :ohmy:

@pgr, please can you move the priority of this in SuiteCRM, professionally we use a lot the Case system of SuiteCRM and this is not fixed for a long time.

best regards

up

up