Skip to content

Commit

Permalink
[#33] Add message template content to activity
Browse files Browse the repository at this point in the history
  • Loading branch information
jofranz committed Feb 21, 2023
1 parent 8bc2b7f commit 0288655
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions CRM/Birthdays/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,20 @@ public function sendMailsAndWriteActivity($contacts, $write_activity): int
try {
$this->sendMail($contact_id, $this->email_address_from, $contact_info['email'], $this->template_id);
if ($write_activity) {
$tpl_info = $this->getMessageTemplate($this->template_id);

$this->createActivity(
$contact_id,
E::ts('Successful birthday greeting mail'),
E::ts(
'Successful birthday greeting mail! Template ID %1 has been used.',
[1 => $this->template_id]
'"%1" birthday message sent successfully',
[1 => $tpl_info['msg_title']]
),
E::ts(
'<b>Subject: %1</b><br><br>%2',
[
1 => $tpl_info['subject'],
2 => $tpl_info['msg_html']
]
)
);
}
Expand Down Expand Up @@ -140,4 +148,29 @@ private function createActivity($target_id, $title, $description): void
Civi::log()->debug(E::LONG_NAME . ' ' . "Unable to write activity: $exception");
}
}

/**
* Returns an array with template data
*
* e.g.:
* [
* 'msg_title'
* 'msg_subject'
* 'msg_html'
* ...
* ]
*
* @throws Exception
*/
private function getMessageTemplate($id): array
{
try {
return \Civi\Api4\MessageTemplate::get()
->addWhere('id', '=', $id)
->execute()
->single();
} catch (UnauthorizedException|CRM_Core_Exception $e) {
throw new Exception(E::LONG_NAME . " " . "Unable to fetch message template $id. Error: $e");
}
}
}

0 comments on commit 0288655

Please sign in to comment.