Skip to content

Instantly share code, notes, and snippets.

@elchele
Created June 26, 2019 21:10
Show Gist options
  • Save elchele/b30577da2bb9bc8685fe97c37f058813 to your computer and use it in GitHub Desktop.
Save elchele/b30577da2bb9bc8685fe97c37f058813 to your computer and use it in GitHub Desktop.
Example on leveraging PMSE Email Template Engine and Sugar Mailer to send parsed email messages
<?php
/* File: ./custom/Extension/modules/Accounts/Ext/LogicHooks/email.ext.php */
$hook_array['after_save'][] = array(
102,
'Send email test',
'custom/modules/Accounts/pmseSend.php',
'emailTest',
'emailTest'
);
?>
<?php
/* File: ./custom/modules/Accounts/pmseSend.php */
require_once('./modules/pmse_Inbox/engine/PMSEHandlers/PMSEBeanHandler.php');
class emailTest{
function emailTest($bean, $event, $args)
{
//Define the template ID (can also be queried)
$template_id = '940cd9e0-56cc-11e9-b791-0800273eeb0f';
//Load the SugarBean object representing the email template
$template = BeanFactory::getBean('pmse_Emails_Templates', $template_id);
//Merge the values from the Bean that triggered the hook into the email template
$beanUtils = new PMSEBeanHandler();
$subject = from_html($beanUtils->mergeBeanInTemplate($bean, $template->subject));
$htmlBody = from_html($beanUtils->mergeBeanInTemplate($bean, $template->body_html));
$txtBody = strip_tags(br2nl($template->body));
//Define params used to create the email msg
$params['toName'] = 'John Doe';
$params['toEmail'] = '[email protected]';
$params['subject'] = $subject;
$params['htmlBody'] = $htmlBody;
$params['txtBody'] = $txtBody;
//Send the message
$this->sendMessage($params);
}
function sendMessage($params)
{
//Instantiate mailer engine, prep message and send
$mailer = MailerFactory::getSystemDefaultMailer();
$email = $params['toEmail'];
$name = $params['toName'];
$mailer->addRecipientsTo(new EmailIdentity($email, $name));
$mailer->setSubject($params['subject']);
$mailer->setHtmlBody($params['htmlBody']);
$mailer->setTextBody($params['txtBody']);
$result = $mailer->send();
//Some code should be added below to check $result and verify msg was delivered to SMTP
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment