Skip to content

Commit

Permalink
Fix Mailing_Event_BAO_Reply to use token processor
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Sep 2, 2022
1 parent 639523a commit 10e75b2
Showing 1 changed file with 26 additions and 45 deletions.
71 changes: 26 additions & 45 deletions CRM/Mailing/Event/BAO/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Token\TokenProcessor;

require_once 'Mail/mime.php';

/**
Expand Down Expand Up @@ -194,23 +196,17 @@ public static function send($queue_id, &$mailing, &$bodyTxt, $replyto, &$bodyHTM
* @param string $replyto
* Optional reply-to from the reply.
*/
private static function autoRespond(&$mailing, $queue_id, $replyto) {
$config = CRM_Core_Config::singleton();

$contacts = CRM_Contact_DAO_Contact::getTableName();
$email = CRM_Core_DAO_Email::getTableName();
$queue = CRM_Mailing_Event_DAO_Queue::getTableName();

$eq = new CRM_Core_DAO();
$eq->query(
"SELECT $contacts.preferred_mail_format as format,
$email.email as email,
$queue.job_id as job_id,
$queue.hash as hash
FROM $contacts
INNER JOIN $queue ON $queue.contact_id = $contacts.id
INNER JOIN $email ON $queue.email_id = $email.id
WHERE $queue.id = " . CRM_Utils_Type::escape($queue_id, 'Integer')
private static function autoRespond(&$mailing, $queue_id, $replyto): void {

$eq = CRM_Core_DAO::executeQuery(
'SELECT civicrm_email.email as email,
civicrm_mailing_event_queue.job_id as job_id,
civicrm_mailing_event_queuee.hash as hash
civicrm_email.contact_id
FROM civicrm_contact
INNER JOIN civicrm_mailing_event_queue ON civicrm_mailing_event_queue.contact_id = civicrm_contact.id
INNER JOIN civicrm_email ON civicrm_mailing_event_queue.email_id = civicrm_email.id
WHERE civicrm_mailing_event_queue.id = ' . CRM_Utils_Type::escape($queue_id, 'Integer')
);
$eq->fetch();

Expand All @@ -220,47 +216,32 @@ private static function autoRespond(&$mailing, $queue_id, $replyto) {
$component->id = $mailing->reply_id;
$component->find(TRUE);

$domain = CRM_Core_BAO_Domain::getDomain();
list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
[$domainEmailName, $domainEmailAddress] = CRM_Core_BAO_Domain::getNameAndEmail();

$params = [
'subject' => $component->subject,
'toEmail' => $to,
'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>",
'replyTo' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
'returnPath' => CRM_Core_BAO_Domain::getNoReplyEmailAddress(),
];

// TODO: do we need reply tokens?
$html = $component->body_html;
if ($component->body_text) {
$text = $component->body_text;
}
else {
$text = CRM_Utils_String::htmlToText($component->body_html);
}

$bao = new CRM_Mailing_BAO_Mailing();
$bao->body_text = $text;
$bao->body_html = $html;
$tokens = $bao->getTokens();

if ($eq->format == 'HTML' || $eq->format == 'Both') {
$html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
$html = CRM_Utils_Token::replaceMailingTokens($html, $mailing, NULL, $tokens['html']);
}
if (!$html || $eq->format == 'Text' || $eq->format == 'Both') {
$text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
$text = CRM_Utils_Token::replaceMailingTokens($text, $mailing, NULL, $tokens['text']);
}
$params['html'] = $html;
$params['text'] = $text;
$html = CRM_Utils_Token::replaceMailingTokens($component->body_html, $mailing, NULL, $component->body_html);
$text = CRM_Utils_Token::replaceMailingTokens($component->body_text, $mailing, NULL, $component->body_text);

CRM_Mailing_BAO_Mailing::addMessageIdHeader($params, 'a', $eq->job_id, $queue_id, $eq->hash);
if (CRM_Core_BAO_MailSettings::includeMessageId()) {
$params['messageId'] = $params['Message-ID'];
}
CRM_Utils_Mail::send($params);
\CRM_Core_BAO_MessageTemplate::sendTemplate([
'envelope' => $params,
'tokenContext' => ['contactId' => $eq->contact_id],
// Template view options
'messageTemplate' => [
'msg_subject' => $component->subject,
'msg_text' => $html,
'msg_html' => $text,
],
]);
}

/**
Expand Down

0 comments on commit 10e75b2

Please sign in to comment.