From 62fae35a62740740fc349989a38121b554ae2513 Mon Sep 17 00:00:00 2001 From: Nathan Tsai Date: Sat, 28 Aug 2021 15:35:44 -0700 Subject: [PATCH] Allow sending of email by details (not user) --- src/EmailApi.php | 94 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 19 deletions(-) diff --git a/src/EmailApi.php b/src/EmailApi.php index 9e1dba2..238bb94 100644 --- a/src/EmailApi.php +++ b/src/EmailApi.php @@ -46,6 +46,16 @@ class EmailApi { */ private $siteEmail; + /** + * @property string Store the manually set body template + */ + private $bodyTemplateOverride; + + /** + * @property string Store the manually set subject template + */ + private $subjectTemplateOverride; + /** * Class constructor. */ @@ -219,7 +229,7 @@ public function notifyUser($userData, string $template, string $destination = '' 'name' => $user->getDisplayName(), 'email' => $user->getEmail(), ]; - $returnResult = $this->notifyEmail($to, $template, $data, $replyTo, $data); + $returnResult = $this->notifyEmail($userData, $template, $data, $replyTo, $data); return $returnResult; } @@ -236,7 +246,7 @@ public function notifyUser($userData, string $template, string $destination = '' * * @return boolean | array */ - public function notifyEmail(array $userData, string $template, string $destination = '', string $replyTo = '', array $options = []) { + public function notifyEmail(array $userData, string $template, string $destination = '', string $replyTo = '', array $data = []) { if (!isset($userData['name'], $userData['email'])) { throw new \Exception('User data does not contain "name" or "email" key.'); @@ -323,25 +333,12 @@ private function sendTemplate(string $to, string $template, $data = [], $replyTo $data['site_name'] = $data['site_name'] ?? $this->siteConfig->get('name'); $data['site_front'] = Url::fromRoute(''); - // Render Subject - $subjectData = [ - '#type' => 'inline_template', - '#template' => $this->emailConfig->get('emails.' . $template . '.subject'), - '#context' => $data, - ]; - $subject = \Drupal::service('renderer')->renderPlain($subjectData); - - // Add body tags to template & render - $bodyTemplate = '' . $this->emailConfig->get('emails.' . $template . '.body') . ''; - $bodyData = [ - '#type' => 'inline_template', - '#template' => $bodyTemplate, - '#context' => $data, - ]; - $body = \Drupal::service('renderer')->renderPlain($bodyData); + // Format subject & body + $subject = $this->getSubjectFromTemplate($template, $data); + $body = $this->getBodyFromTemplate($template, $data); // Set the message - $message['to'] = $to; + $message['to'] = $to; $message['subject'] = $subject; $message['body'][] = $body; $message['headers'] = $this->getHeaders($replyTo); @@ -362,6 +359,65 @@ private function sendTemplate(string $to, string $template, $data = [], $replyTo return $result; } + + /** + * Allow the body template to be override + * @TODO Refactor to reduce the number of parameters passed down functions + */ + public function overrideBodyTemplate(string $bodyTemplateOverride) { + $this->bodyTemplateOverride = $bodyTemplateOverride; + } + + /** + * Format the Template & allow for overrides + */ + private function getBodyFromTemplate(string $template, array $data) { + $templateText = ''; + if (!empty($this->bodyTemplateOverride)) { + $templateText = $this->bodyTemplateOverride; + } + else { + $templateText = $this->emailConfig->get('emails.' . $template . '.body'); + } + + $templateText = '' . $templateText. ''; + $render = [ + '#type' => 'inline_template', + '#template' => $templateText, + '#context' => $data, + ]; + + return \Drupal::service('renderer')->renderPlain($render);; + } + + /** + * Allow the subject template to be override + * @TODO Refactor to reduce the number of parameters passed down functions + */ + public function overrideSubjectTemplate(string $subjectTemplateOverride) { + $this->subjectTemplateOverride = $subjectTemplateOverride; + } + + /** + * Format the Template & allow for overrides + */ + private function getSubjectFromTemplate(string $template, array $data) { + $templateText = ''; + if (!empty($this->subjectTemplateOverride)) { + $templateText = $this->subjectTemplateOverride; + } + else { + $templateText = $this->emailConfig->get('emails.' . $template . '.subject'); + } + + $render = [ + '#type' => 'inline_template', + '#template' => $templateText, + '#context' => $data, + ]; + + return \Drupal::service('renderer')->renderPlain($render);; + } /** * Private function that returns the headers for the emails