Skip to content

Commit

Permalink
Allow sending of email by details (not user)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishappy committed Aug 28, 2021
1 parent af9262c commit 62fae35
Showing 1 changed file with 75 additions and 19 deletions.
94 changes: 75 additions & 19 deletions src/EmailApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.');
Expand Down Expand Up @@ -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('<front>');

// 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 = '<body style="font-size: 14px; color: #000;">' . $this->emailConfig->get('emails.' . $template . '.body') . '</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);
Expand All @@ -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 = '<body style="font-size: 14px; color: #000;">' . $templateText. '</body>';
$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
Expand Down

0 comments on commit 62fae35

Please sign in to comment.