Skip to content

Instantly share code, notes, and snippets.

Created June 26, 2019 21:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Example on leveraging PMSE Email Template Engine and Sugar Mailer to send parsed email messages
/* File: ./custom/Extension/modules/Accounts/Ext/LogicHooks/email.ext.php */
$hook_array['after_save'][] = array(
'Send email test',
/* File: ./custom/modules/Accounts/pmseSend.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'] = '';
$params['subject'] = $subject;
$params['htmlBody'] = $htmlBody;
$params['txtBody'] = $txtBody;
//Send the message
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));
$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