Exemplo n.º 1
0
 /**
  * @param int $contactId
  * @param $email
  * @param int $messageTemplateID
  * @param $from
  *
  * @return bool|NULL
  */
 public static function sendReminder($contactId, $email, $messageTemplateID, $from)
 {
     $messageTemplates = new CRM_Core_DAO_MessageTemplate();
     $messageTemplates->id = $messageTemplateID;
     $domain = CRM_Core_BAO_Domain::getDomain();
     $result = NULL;
     $hookTokens = array();
     if ($messageTemplates->find(TRUE)) {
         $body_text = $messageTemplates->msg_text;
         $body_html = $messageTemplates->msg_html;
         $body_subject = $messageTemplates->msg_subject;
         if (!$body_text) {
             $body_text = CRM_Utils_String::htmlToText($body_html);
         }
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         //CRM-5734
         // get tokens to be replaced
         $tokens = array_merge(CRM_Utils_Token::getTokens($body_text), CRM_Utils_Token::getTokens($body_html), CRM_Utils_Token::getTokens($body_subject));
         // get replacement text for these tokens
         $returnProperties = array("preferred_mail_format" => 1);
         if (isset($tokens['contact'])) {
             foreach ($tokens['contact'] as $key => $value) {
                 $returnProperties[$value] = 1;
             }
         }
         list($details) = CRM_Utils_Token::getTokenDetails(array($contactId), $returnProperties, NULL, NULL, FALSE, $tokens, 'CRM_Core_BAO_MessageTemplate');
         $contact = reset($details);
         // call token hook
         $hookTokens = array();
         CRM_Utils_Hook::tokens($hookTokens);
         $categories = array_keys($hookTokens);
         // do replacements in text and html body
         $type = array('html', 'text');
         foreach ($type as $key => $value) {
             $bodyType = "body_{$value}";
             if (${$bodyType}) {
                 CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']);
                 ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens, FALSE, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE);
             }
         }
         $html = $body_html;
         $text = $body_text;
         $smarty = CRM_Core_Smarty::singleton();
         foreach (array('text', 'html') as $elem) {
             ${$elem} = $smarty->fetch("string:{${$elem}}");
         }
         // do replacements in message subject
         $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $tokens);
         $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $tokens);
         $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens, TRUE);
         $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
         $messageSubject = $smarty->fetch("string:{$messageSubject}");
         // set up the parameters for CRM_Utils_Mail::send
         $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject);
         if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
             // render the & entities in text mode, so that the links work
             $mailParams['text'] = str_replace('&', '&', $text);
         }
         if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
             $mailParams['html'] = $html;
         }
         $result = CRM_Utils_Mail::send($mailParams);
     }
     $messageTemplates->free();
     return $result;
 }