From 1d91f6d1c4935857ef5ef486034d0494b50f9f37 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 27 Mar 2020 10:30:31 +0100 Subject: [PATCH] Improved rendering for email templates (#611). --- .../views/emails/appointment_details.php | 52 +++---- .../views/emails/delete_appointment.php | 52 +++---- engine/Notifications/Email.php | 132 ++++++------------ 3 files changed, 91 insertions(+), 145 deletions(-) diff --git a/application/views/emails/appointment_details.php b/application/views/emails/appointment_details.php index 229fff7b..b4f6594b 100644 --- a/application/views/emails/appointment_details.php +++ b/application/views/emails/appointment_details.php @@ -1,6 +1,6 @@ - Appointment Details + <?= lang('appointment_details_title') ?>
@@ -8,63 +8,63 @@ height: 45px; padding: 10px 15px;"> +
-

$email_title

-

$email_message

+

+

-

Appointment Details

+

- - + + - - + + - - + + - - + +
Service$appointment_service
Provider$appointment_provider
Start$appointment_start_date
End$appointment_end_date
-

Customer Details

+

- - + + - - + + - - + + - - + +
Name$customer_name
Email$customer_email
Phone$customer_phone
Address$customer_address
-

Appointment Link

- $appointment_link +

+
diff --git a/application/views/emails/delete_appointment.php b/application/views/emails/delete_appointment.php index 0079aab6..192fa334 100644 --- a/application/views/emails/delete_appointment.php +++ b/application/views/emails/delete_appointment.php @@ -1,6 +1,6 @@ - $email_title + <?= lang('appointment_cancelled_title') ?>
@@ -8,63 +8,63 @@ height: 45px; padding: 10px 15px;"> +
-

$email_title

-

$email_message

+

+

-

Appointment Details

+

- - + + - - + + - - + + - - + +
Service$appointment_service
Provider$appointment_provider
Date$appointment_date
Duration$appointment_duration
-

Customer Details

+

- - + + - - + + - - + + - - + +
Name$customer_name
Email$customer_email
Phone$customer_phone
Address$customer_address
-

Reason

-

$reason

+

+

diff --git a/engine/Notifications/Email.php b/engine/Notifications/Email.php index c900363b..f2dc71b1 100755 --- a/engine/Notifications/Email.php +++ b/engine/Notifications/Email.php @@ -52,27 +52,6 @@ class Email { $this->config = $config; } - /** - * Replace the email template variables. - * - * This method finds and replaces the html variables of an email template. It is used to - * generate dynamic HTML emails that are send as notifications to the system users. - * - * @param array $replaceArray Array that contains the variables to be replaced. - * @param string $templateHtml The email template HTML. - * - * @return string Returns the new email html that contain the variables of the $replaceArray. - */ - protected function _replaceTemplateVariables(array $replaceArray, $templateHtml) - { - foreach ($replaceArray as $name => $value) - { - $templateHtml = str_replace($name, $value, $templateHtml); - } - - return $templateHtml; - } - /** * Send an email with the appointment details. * @@ -132,46 +111,30 @@ class Email { } // Prepare template replace array. - $replaceArray = [ - '$email_title' => $title->get(), - '$email_message' => $message->get(), - '$appointment_service' => $service['name'], - '$appointment_provider' => $provider['first_name'] . ' ' . $provider['last_name'], - '$appointment_start_date' => date($date_format . ' ' . $timeFormat, strtotime($appointment['start_datetime'])), - '$appointment_end_date' => date($date_format . ' ' . $timeFormat, strtotime($appointment['end_datetime'])), - '$appointment_link' => $appointmentLink->get(), - '$company_link' => $company['company_link'], - '$company_name' => $company['company_name'], - '$customer_name' => $customer['first_name'] . ' ' . $customer['last_name'], - '$customer_email' => $customer['email'], - '$customer_phone' => $customer['phone_number'], - '$customer_address' => $customer['address'], + $email_title = $title->get(); + $email_message = $message->get(); + $appointment_service = $service['name']; + $appointment_provider = $provider['first_name'] . ' ' . $provider['last_name']; + $appointment_start_date = date($date_format . ' ' . $timeFormat, strtotime($appointment['start_datetime'])); + $appointment_end_date = date($date_format . ' ' . $timeFormat, strtotime($appointment['end_datetime'])); + $appointment_link = $appointmentLink->get(); + $company_link = $company['company_link']; + $company_name = $company['company_name']; + $customer_name = $customer['first_name'] . ' ' . $customer['last_name']; + $customer_email = $customer['email']; + $customer_phone = $customer['phone_number']; + $customer_address = $customer['address']; - // Translations - 'Appointment Details' => $this->framework->lang->line('appointment_details_title'), - 'Service' => $this->framework->lang->line('service'), - 'Provider' => $this->framework->lang->line('provider'), - 'Start' => $this->framework->lang->line('start'), - 'End' => $this->framework->lang->line('end'), - 'Customer Details' => $this->framework->lang->line('customer_details_title'), - 'Name' => $this->framework->lang->line('name'), - 'Email' => $this->framework->lang->line('email'), - 'Phone' => $this->framework->lang->line('phone_number'), - 'Address' => $this->framework->lang->line('address'), - 'Appointment Link' => $this->framework->lang->line('appointment_link_title') - ]; - - $html = file_get_contents(__DIR__ . '/../../application/views/emails/appointment_details.php'); - $html = $this->_replaceTemplateVariables($replaceArray, $html); + ob_start(); + require __DIR__ . '/../../application/views/emails/appointment_details.php'; + $html = ob_get_clean(); $mailer = $this->_createMailer(); - $mailer->From = $company['company_email']; $mailer->FromName = $company['company_name']; $mailer->AddAddress($recipientEmail->get()); $mailer->Subject = $title->get(); $mailer->Body = $html; - $mailer->addStringAttachment($icsStream->get(), 'invitation.ics'); if ( ! $mailer->Send()) @@ -236,37 +199,21 @@ class Email { } // Prepare email template data. - $replaceArray = [ - '$email_title' => $this->framework->lang->line('appointment_cancelled_title'), - '$email_message' => $this->framework->lang->line('appointment_removed_from_schedule'), - '$appointment_service' => $service['name'], - '$appointment_provider' => $provider['first_name'] . ' ' . $provider['last_name'], - '$appointment_date' => date($date_format . ' ' . $timeFormat, strtotime($appointment['start_datetime'])), - '$appointment_duration' => $service['duration'] . ' ' . $this->framework->lang->line('minutes'), - '$company_link' => $company['company_link'], - '$company_name' => $company['company_name'], - '$customer_name' => $customer['first_name'] . ' ' . $customer['last_name'], - '$customer_email' => $customer['email'], - '$customer_phone' => $customer['phone_number'], - '$customer_address' => $customer['address'], - '$reason' => $reason->get(), + $appointment_service = $service['name']; + $appointment_provider = $provider['first_name'] . ' ' . $provider['last_name']; + $appointment_date = date($date_format . ' ' . $timeFormat, strtotime($appointment['start_datetime'])); + $appointment_duration = $service['duration'] . ' ' . $this->framework->lang->line('minutes'); + $company_link = $company['company_link']; + $company_name = $company['company_name']; + $customer_name = $customer['first_name'] . ' ' . $customer['last_name']; + $customer_email = $customer['email']; + $customer_phone = $customer['phone_number']; + $customer_address = $customer['address']; + $reason = $reason->get(); - // Translations - 'Appointment Details' => $this->framework->lang->line('appointment_details_title'), - 'Service' => $this->framework->lang->line('service'), - 'Provider' => $this->framework->lang->line('provider'), - 'Date' => $this->framework->lang->line('start'), - 'Duration' => $this->framework->lang->line('duration'), - 'Customer Details' => $this->framework->lang->line('customer_details_title'), - 'Name' => $this->framework->lang->line('name'), - 'Email' => $this->framework->lang->line('email'), - 'Phone' => $this->framework->lang->line('phone_number'), - 'Address' => $this->framework->lang->line('address'), - 'Reason' => $this->framework->lang->line('reason') - ]; - - $html = file_get_contents(__DIR__ . '/../../application/views/emails/delete_appointment.php'); - $html = $this->_replaceTemplateVariables($replaceArray, $html); + ob_start(); + require __DIR__ . '/../../application/views/emails/delete_appointment.php'; + $html = ob_get_clean(); $mailer = $this->_createMailer(); @@ -293,17 +240,16 @@ class Email { */ public function sendPassword(NonEmptyText $password, EmailAddress $recipientEmail, array $company) { - $replaceArray = [ - '$email_title' => $this->framework->lang->line('new_account_password'), - '$email_message' => $this->framework->lang->line('new_password_is'), - '$company_name' => $company['company_name'], - '$company_email' => $company['company_email'], - '$company_link' => $company['company_link'], - '$password' => '' . $password->get() . '' - ]; + $email_title = $this->framework->lang->line('new_account_password'); + $email_message = $this->framework->lang->line('new_password_is'); + $company_name = $company['company_name']; + $company_email = $company['company_email']; + $company_link = $company['company_link']; + $password = '' . $password->get() . ''; - $html = file_get_contents(__DIR__ . '/../../application/views/emails/new_password.php'); - $html = $this->_replaceTemplateVariables($replaceArray, $html); + ob_start(); + require __DIR__ . '/../../application/views/emails/new_password.php'; + $html = ob_get_clean(); $mailer = $this->_createMailer();