From 60711539f0fe8372bd4d7f53a91d479db8507880 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 28 Sep 2021 10:15:16 +1300 Subject: [PATCH] [REF] Copy another email trait function back to the trait The only core use for this function is via the task & the signature is cludgey This copies it back & deprecates the no-longer-in-use code --- CRM/Activity/BAO/Activity.php | 4 ++ CRM/Contact/Form/Task/EmailTrait.php | 79 +++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index f94545a0ba..6ea357f9fb 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -1389,6 +1389,8 @@ WHERE entity_id =%1 AND entity_table = %2"; } /** + * DO Not use this function. Under deprecation, no active core use. + * * Send the message to a specific contact. * * @param string $from @@ -1410,6 +1412,8 @@ WHERE entity_id =%1 AND entity_table = %2"; * * @return bool * TRUE if successful else FALSE. + * + * @deprecated */ public static function sendMessage( $from, diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index 10054718f5..da674268c3 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -889,7 +889,7 @@ trait CRM_Contact_Form_Task_EmailTrait { $firstActivityCreated = TRUE; } - if (CRM_Activity_BAO_Activity::sendMessage( + if ($this->sendMessage( $from, $userID, $contactId, @@ -911,4 +911,81 @@ trait CRM_Contact_Form_Task_EmailTrait { return [$sent, $activityIds]; } + /** + * Send message - under refactor. + * + * @param $from + * @param $fromID + * @param $toID + * @param $subject + * @param $text_message + * @param $html_message + * @param $emailAddress + * @param $activityID + * @param null $attachments + * @param null $cc + * @param null $bcc + * + * @return bool + * @throws \CRM_Core_Exception + * @throws \PEAR_Exception + */ + protected function sendMessage( + $from, + $fromID, + $toID, + &$subject, + &$text_message, + &$html_message, + $emailAddress, + $activityID, + $attachments = NULL, + $cc = NULL, + $bcc = NULL + ) { + [$toDisplayName, $toEmail, $toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($toID); + if ($emailAddress) { + $toEmail = trim($emailAddress); + } + + // make sure both email addresses are valid + // and that the recipient wants to receive email + if (empty($toEmail) or $toDoNotEmail) { + return FALSE; + } + if (!trim($toDisplayName)) { + $toDisplayName = $toEmail; + } + + $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); + $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); + + // create the params array + $mailParams = [ + 'groupName' => 'Activity Email Sender', + 'from' => $from, + 'toName' => $toDisplayName, + 'toEmail' => $toEmail, + 'subject' => $subject, + 'cc' => $cc, + 'bcc' => $bcc, + 'text' => $text_message, + 'html' => $html_message, + 'attachments' => $attachments, + ]; + + if (!CRM_Utils_Mail::send($mailParams)) { + return FALSE; + } + + // add activity target record for every mail that is send + $activityTargetParams = [ + 'activity_id' => $activityID, + 'contact_id' => $toID, + 'record_type_id' => $targetID, + ]; + CRM_Activity_BAO_ActivityContact::create($activityTargetParams); + return TRUE; + } + } -- 2.25.1