SugarBPM - Extending PMSEEmailHandler

Hi,

I've been trying to extend PMSEEmailHandler.php, located in modules/pmse_Inbox/engine/PMSEHandlers/PMSEEmailHandler.php. What I'm trying to accomplish is creating an Email record of the email that is being sent by a process and relate it to the record that triggered this process, so it can exist as an outbound email record as if it was sent manually from the Emails subpanel within that record.

According to this documentation it's possible to extend this object and it should be enough with creating a custom/modules/pmse_Inbox/engine/PMSEHandlers/CustomPMSEEmailHandler.php file extending PMSEEmailHandler. That's what I've done and to test it I just ovewrote the sendTemplateEmail function adding a log in line 10 as follows:

<?php

require_once('modules/pmse_Inbox/engine/PMSEHandlers/PMSEEmailHandler.php');

use Sugarcrm\Sugarcrm\ProcessManager;

class CustomPMSEEmailHandler extends PMSEEmailHandler {

    public function sendTemplateEmail($moduleName, $beanId, $addresses, $templateId) {
        $GLOBALS['log']->fatal('sendTemplateEmail');
        $mailTransmissionProtocol = "unknown";
        if (PMSEEngineUtils::isEmailRecipientEmpty($addresses)) {
            $this->getLogger()->alert('All email recipients are filtered out of the email recipient list.');
            return;
        }
        try {
            $bean = $this->retrieveBean($moduleName, $beanId);
            $templateObject = $this->retrieveBean('pmse_Emails_Templates');
            $templateObject->disable_row_level_security = true;

            $mailObject = $this->retrieveMailer();
            $mailTransmissionProtocol   = $mailObject->getMailTransmissionProtocol();

            $this->addRecipients($mailObject, $addresses);

            if (isset($templateId) && $templateId != "") {
                $templateObject->retrieve($templateId);
            } else {
                $this->getLogger()->warning('template_id is not defined');
            }

            if (!empty($templateObject->from_name) && !empty($templateObject->from_address)) {
                $mailObject->setHeader(EmailHeaders::From, new EmailIdentity($templateObject->from_address, $templateObject->from_name));
            }

            $sender = $this->getSender($templateObject);

            if (isset($sender)) {
                $mailObject->setHeader(EmailHeaders::From, new EmailIdentity($sender['address'], $sender['name']));
            }

            $emailBody = $this->getEmailBody($templateObject, $bean);
            $mailObject->setHtmlBody($emailBody['htmlBody']);
            $mailObject->setTextBody($emailBody['textBody']);

            $mailObject->setSubject($this->getSubject($templateObject, $bean));

            $mailObject->send();
        } catch (MailerException $mailerException) {
            $message = $mailerException->getMessage();
            $this->getLogger()->warning("Error sending email (method: {$mailTransmissionProtocol}), (error: {$message})");
        }
    }
}

After uploading this file the process emails are still being sent (at least I didn't break anything) but nothing is being logged. As I see it there are many possible causes for this: the file or the function I extended may not be the right one, there may be something else I need to extend in order for this to work, etcetera.

I'm doing this with a Sugar Enterprise 9.3.0 instance in Sugar Cloud.

If anyone has any experience with this or has any advice it will be greatly appreciated.

Kind regards,

Rodrigo

Parents Reply Children
  • For anyone looking to do something similar, I had to override sendEmailFromQueue() not sendTemplateEmail().

    custom/modules/pmse_Inbox/engine/PMSEHandlers/CustomPMSEEmailHandler.php

    <?php

    require_once('modules/pmse_Inbox/engine/PMSEHandlers/PMSEEmailHandler.php');

    use Sugarcrm\Sugarcrm\ProcessManager;

    class CustomPMSEEmailHandler extends PMSEEmailHandler {

        public function sendEmailFromQueue(pmse_EmailMessage $email)

        {

            ....

            $mailObject->setHeader('Importance', 'High');

            ....

        }

    }