2021-11-06 18:11:44 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-11-06 18:11:44 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2021-11-06 18:11:44 +03:00
|
|
|
* @since v1.4.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
|
2021-11-06 18:11:44 +03:00
|
|
|
/**
|
|
|
|
* Email messages library.
|
|
|
|
*
|
|
|
|
* Handles the email messaging related functionality.
|
|
|
|
*
|
|
|
|
* @package Libraries
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Email_messages
|
|
|
|
{
|
2021-11-06 18:11:44 +03:00
|
|
|
/**
|
2023-03-13 11:06:18 +03:00
|
|
|
* @var EA_Controller|CI_Controller
|
2021-11-06 18:11:44 +03:00
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected EA_Controller|CI_Controller $CI;
|
2021-11-06 18:11:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Email_messages constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->CI = &get_instance();
|
2021-11-06 18:11:44 +03:00
|
|
|
|
|
|
|
$this->CI->load->model('admins_model');
|
|
|
|
$this->CI->load->model('appointments_model');
|
|
|
|
$this->CI->load->model('providers_model');
|
|
|
|
$this->CI->load->model('secretaries_model');
|
|
|
|
$this->CI->load->model('secretaries_model');
|
|
|
|
$this->CI->load->model('settings_model');
|
|
|
|
|
|
|
|
$this->CI->load->library('email');
|
|
|
|
$this->CI->load->library('ics_file');
|
|
|
|
$this->CI->load->library('timezones');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email with the appointment details.
|
|
|
|
*
|
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param array $service Service data.
|
|
|
|
* @param array $customer Customer data.
|
|
|
|
* @param array $settings App settings.
|
|
|
|
* @param string $subject Email subject.
|
|
|
|
* @param string $message Email message.
|
2023-02-21 09:58:21 +03:00
|
|
|
* @param string $appointment_link Appointment unique URL.
|
2021-11-06 18:11:44 +03:00
|
|
|
* @param string $recipient_email Recipient email address.
|
|
|
|
* @param string $ics_stream ICS file contents.
|
|
|
|
* @param string|null $timezone Custom timezone.
|
|
|
|
*
|
2024-11-21 18:51:31 +03:00
|
|
|
* @throws DateInvalidTimeZoneException
|
|
|
|
* @throws DateMalformedStringException
|
2021-11-06 18:11:44 +03:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2023-05-04 19:02:40 +03:00
|
|
|
public function send_appointment_saved(
|
2021-11-06 18:11:44 +03:00
|
|
|
array $appointment,
|
|
|
|
array $provider,
|
|
|
|
array $service,
|
|
|
|
array $customer,
|
|
|
|
array $settings,
|
|
|
|
string $subject,
|
|
|
|
string $message,
|
2023-02-21 09:58:21 +03:00
|
|
|
string $appointment_link,
|
2021-11-06 18:11:44 +03:00
|
|
|
string $recipient_email,
|
|
|
|
string $ics_stream,
|
2024-12-19 21:13:51 +03:00
|
|
|
?string $timezone = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
): void {
|
2021-11-06 18:11:44 +03:00
|
|
|
$appointment_timezone = new DateTimeZone($provider['timezone']);
|
|
|
|
|
|
|
|
$appointment_start = new DateTime($appointment['start_datetime'], $appointment_timezone);
|
|
|
|
|
|
|
|
$appointment_end = new DateTime($appointment['end_datetime'], $appointment_timezone);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($timezone && $timezone !== $provider['timezone']) {
|
2023-05-04 19:09:57 +03:00
|
|
|
$custom_timezone = new DateTimeZone($timezone);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2023-05-04 19:09:57 +03:00
|
|
|
$appointment_start->setTimezone($custom_timezone);
|
|
|
|
$appointment['start_datetime'] = $appointment_start->format('Y-m-d H:i:s');
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2023-05-04 19:09:57 +03:00
|
|
|
$appointment_end->setTimezone($custom_timezone);
|
|
|
|
$appointment['end_datetime'] = $appointment_end->format('Y-m-d H:i:s');
|
2021-11-06 18:11:44 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$html = $this->CI->load->view(
|
|
|
|
'emails/appointment_saved_email',
|
|
|
|
[
|
|
|
|
'subject' => $subject,
|
|
|
|
'message' => $message,
|
|
|
|
'appointment' => $appointment,
|
|
|
|
'service' => $service,
|
|
|
|
'provider' => $provider,
|
|
|
|
'customer' => $customer,
|
|
|
|
'settings' => $settings,
|
|
|
|
'timezone' => $timezone,
|
2023-12-22 13:35:41 +03:00
|
|
|
'appointment_link' => $appointment_link,
|
2023-11-29 12:24:09 +03:00
|
|
|
],
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-27 14:08:49 +03:00
|
|
|
$php_mailer = $this->get_php_mailer($recipient_email, $subject, $html);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-12-09 13:22:03 +03:00
|
|
|
$php_mailer->addStringAttachment($ics_stream, 'invitation.ics', PHPMailer::ENCODING_BASE64, 'text/calendar');
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
$php_mailer->send();
|
2021-11-06 18:11:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email with the appointment removal details.
|
|
|
|
*
|
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param array $service Service data.
|
|
|
|
* @param array $customer Customer data.
|
|
|
|
* @param array $settings App settings.
|
|
|
|
* @param string $recipient_email Recipient email address.
|
|
|
|
* @param string|null $reason Removal reason.
|
|
|
|
* @param string|null $timezone Custom timezone.
|
|
|
|
*
|
2024-11-21 18:51:31 +03:00
|
|
|
* @throws DateInvalidTimeZoneException
|
|
|
|
* @throws DateMalformedStringException
|
2021-11-06 18:11:44 +03:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2023-05-04 19:02:40 +03:00
|
|
|
public function send_appointment_deleted(
|
2021-11-06 18:11:44 +03:00
|
|
|
array $appointment,
|
|
|
|
array $provider,
|
|
|
|
array $service,
|
|
|
|
array $customer,
|
|
|
|
array $settings,
|
|
|
|
string $recipient_email,
|
2024-12-19 21:13:51 +03:00
|
|
|
?string $reason = null,
|
|
|
|
?string $timezone = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
): void {
|
2021-11-06 18:11:44 +03:00
|
|
|
$appointment_timezone = new DateTimeZone($provider['timezone']);
|
|
|
|
|
|
|
|
$appointment_start = new DateTime($appointment['start_datetime'], $appointment_timezone);
|
|
|
|
|
2023-05-04 19:09:57 +03:00
|
|
|
$appointment_end = new DateTime($appointment['end_datetime'], $appointment_timezone);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($timezone && $timezone !== $provider['timezone']) {
|
2023-05-04 19:09:57 +03:00
|
|
|
$custom_timezone = new DateTimeZone($timezone);
|
|
|
|
|
|
|
|
$appointment_start->setTimezone($custom_timezone);
|
|
|
|
$appointment['start_datetime'] = $appointment_start->format('Y-m-d H:i:s');
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2023-05-04 19:09:57 +03:00
|
|
|
$appointment_end->setTimezone($custom_timezone);
|
|
|
|
$appointment['end_datetime'] = $appointment_end->format('Y-m-d H:i:s');
|
2021-11-06 18:11:44 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$html = $this->CI->load->view(
|
|
|
|
'emails/appointment_deleted_email',
|
|
|
|
[
|
|
|
|
'appointment' => $appointment,
|
|
|
|
'service' => $service,
|
|
|
|
'provider' => $provider,
|
|
|
|
'customer' => $customer,
|
|
|
|
'settings' => $settings,
|
|
|
|
'timezone' => $timezone,
|
2023-12-22 13:35:41 +03:00
|
|
|
'reason' => $reason,
|
2023-11-29 12:24:09 +03:00
|
|
|
],
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
$subject = lang('appointment_cancelled_title');
|
2024-03-04 12:59:52 +03:00
|
|
|
|
2024-11-27 14:08:49 +03:00
|
|
|
$php_mailer = $this->get_php_mailer($recipient_email, $subject, $html);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
$php_mailer->send();
|
2021-11-06 18:11:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the account recovery details.
|
|
|
|
*
|
|
|
|
* @param string $password New password.
|
|
|
|
* @param string $recipient_email Recipient email address.
|
|
|
|
* @param array $settings App settings.
|
2024-11-21 18:51:31 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2021-11-06 18:11:44 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function send_password(string $password, string $recipient_email, array $settings): void
|
2021-11-06 18:11:44 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$html = $this->CI->load->view(
|
|
|
|
'emails/account_recovery_email',
|
|
|
|
[
|
|
|
|
'subject' => lang('new_account_password'),
|
|
|
|
'message' => str_replace('$password', '<strong>' . $password . '</strong>', lang('new_password_is')),
|
2023-12-22 13:35:41 +03:00
|
|
|
'settings' => $settings,
|
2023-11-29 12:24:09 +03:00
|
|
|
],
|
2023-12-22 13:35:41 +03:00
|
|
|
true,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
$subject = lang('new_account_password');
|
2024-03-04 12:59:52 +03:00
|
|
|
|
2024-11-27 14:08:49 +03:00
|
|
|
$php_mailer = $this->get_php_mailer($recipient_email, $subject, $html);
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
$php_mailer->send();
|
|
|
|
}
|
2021-11-06 18:11:44 +03:00
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
/**
|
|
|
|
* Create PHP Mailer instance based on the email configuration.
|
|
|
|
*
|
2024-11-27 14:08:49 +03:00
|
|
|
* @param string|null $recipient_email
|
|
|
|
* @param string|null $subject
|
|
|
|
* @param string|null $html
|
|
|
|
*
|
2024-11-21 18:51:31 +03:00
|
|
|
* @return PHPMailer
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-11-27 14:08:49 +03:00
|
|
|
private function get_php_mailer(
|
2024-12-19 21:13:51 +03:00
|
|
|
?string $recipient_email = null,
|
|
|
|
?string $subject = null,
|
|
|
|
?string $html = null,
|
2024-11-27 14:08:49 +03:00
|
|
|
): PHPMailer {
|
2024-11-21 18:51:31 +03:00
|
|
|
$php_mailer = new PHPMailer(true);
|
|
|
|
|
2024-11-27 13:24:18 +03:00
|
|
|
$php_mailer->isHTML();
|
2024-11-22 14:57:02 +03:00
|
|
|
$php_mailer->CharSet = 'UTF-8';
|
2024-11-21 18:51:31 +03:00
|
|
|
$php_mailer->SMTPDebug = config('smtp_debug') ? SMTP::DEBUG_SERVER : null;
|
|
|
|
|
|
|
|
if (config('protocol') === 'smtp') {
|
|
|
|
$php_mailer->isSMTP();
|
|
|
|
$php_mailer->Host = config('smtp_host');
|
|
|
|
$php_mailer->SMTPAuth = config('smtp_auth');
|
|
|
|
$php_mailer->Username = config('smtp_user');
|
|
|
|
$php_mailer->Password = config('smtp_pass');
|
|
|
|
$php_mailer->SMTPSecure = config('smtp_crypto');
|
|
|
|
$php_mailer->Port = config('smtp_port');
|
2021-11-06 18:11:44 +03:00
|
|
|
}
|
2024-11-21 18:51:31 +03:00
|
|
|
|
|
|
|
$from_name = config('from_name') ?: setting('company_name');
|
|
|
|
$from_address = config('from_address') ?: setting('company_email');
|
|
|
|
$reply_to_address = config('reply_to') ?: setting('company_email');
|
|
|
|
|
|
|
|
$php_mailer->setFrom($from_address, $from_name);
|
|
|
|
$php_mailer->addReplyTo($reply_to_address);
|
|
|
|
|
2024-11-27 14:08:49 +03:00
|
|
|
if ($recipient_email) {
|
|
|
|
$php_mailer->addAddress($recipient_email);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($subject) {
|
|
|
|
$php_mailer->Subject = $subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($html) {
|
|
|
|
$php_mailer->Body = $html;
|
|
|
|
$php_mailer->AltBody = $html;
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:51:31 +03:00
|
|
|
return $php_mailer;
|
2021-11-06 18:11:44 +03:00
|
|
|
}
|
|
|
|
}
|