diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index fbbb96709e01..4dedb8c66e7d 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -402,6 +402,8 @@ public static function sendTemplate($params) { 'isTest' => FALSE, // filename of optional PDF version to add as attachment (do not include path) 'PDFFilename' => NULL, + // Disable Smarty? + 'disableSmarty' => FALSE, ]; $params = array_merge($defaults, $params); @@ -511,14 +513,17 @@ public static function sendTemplate($params) { $contact = $contact[$contactID]; } - $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], TRUE); - $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE); - $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE); + // When using Smarty we need to pass the $escapeSmarty parameter. + $escapeSmarty = !$params['disableSmarty']; + + $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], $escapeSmarty); + $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], $escapeSmarty); + $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], $escapeSmarty); if ($contactID) { - $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['subject'], FALSE, TRUE); - $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE); - $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE); + $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['subject'], FALSE, $escapeSmarty); + $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, $escapeSmarty); + $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, $escapeSmarty); $contactArray = [$contactID => $contact]; CRM_Utils_Hook::tokenValues($contactArray, @@ -535,20 +540,30 @@ public static function sendTemplate($params) { $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE); } - // strip whitespace from ends and turn into a single line - $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}"; + // Normally Smarty is run, but it can be disabled using the disableSmarty + // parameter, which may be useful for non-core uses of MessageTemplate.send + // In particular it helps with the mosaicomsgtpl extension. + if (!$params['disableSmarty']) { + // strip whitespace from ends and turn into a single line + $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}"; - // parse the three elements with Smarty - $smarty = CRM_Core_Smarty::singleton(); - foreach ($params['tplParams'] as $name => $value) { - $smarty->assign($name, $value); + // parse the three elements with Smarty + $smarty = CRM_Core_Smarty::singleton(); + foreach ($params['tplParams'] as $name => $value) { + $smarty->assign($name, $value); + } + foreach ([ + 'subject', + 'text', + 'html', + ] as $elem) { + $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}"); + } } - foreach ([ - 'subject', - 'text', - 'html', - ] as $elem) { - $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}"); + else { + // Since we're not relying on Smarty for this function, we DIY. + // strip whitespace from ends and turn into a single line + $mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject'])); } // send the template, honouring the target user’s preferences (if any) diff --git a/api/v3/MessageTemplate.php b/api/v3/MessageTemplate.php index 8f33ce88c595..b75e43df5b32 100644 --- a/api/v3/MessageTemplate.php +++ b/api/v3/MessageTemplate.php @@ -181,4 +181,9 @@ function _civicrm_api3_message_template_send_spec(&$params) { $params['pdf_filename']['title'] = 'PDF Filename'; $params['pdf_filename']['api.aliases'] = ['PDFFilename']; $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING; + + $params['disable_smarty']['description'] = 'Disable Smarty. Normal CiviMail tokens are still supported. By default Smarty is enabled.'; + $params['disable_smarty']['title'] = 'Disable Smarty'; + $params['disable_smarty']['api.aliases'] = ['disableSmarty']; + $params['disable_smarty']['type'] = CRM_Utils_Type::T_BOOLEAN; }