'); $xml->record->user_name = $user['user_first_name'] ?: $user['user_name']; $body = $this->applyXSL($xml, $this->getXSL('emailPasswordResetLink'), ['RESET_URL' => $url]); return $this->sendEmail((string) $user['user_email'], self::APPLICATION_NAME . ' - Password reset', $body); } // Variables. private $current_user_name = ""; private $current_user_email = ""; // ================== // Class Constructor. // ================== public function __construct() { set_error_handler(array($this, "displayError")); date_default_timezone_set(self::TIMEZONE); $this->current_user_name = $_SESSION[self::USER_NAME] ?? ""; $this->current_user_email = $_SESSION[self::USER_EMAIL] ?? ""; } // ============================= // Email a new users credentials // ============================= public function emailNewUserCredentials($user, $password = null) { $this->emailUserPasswordResetLink($user); } public function emailUserPasswordResetLink($user, $password = null) { if ($user instanceof SimpleXMLElement) { (new AuthService())->requestReset((string) $user->record->user_email); } } // ======================================= // Send an email with optional attachments // ======================================= public function sendEmail($to = "", $subject = "", $message = "", $attachments = "", $attachment_names = "unknown") { if (empty($to) or empty($subject) or empty($message)) { return false; } $settings = $this->getSettings(); $SmtpHost = $settings->SmtpServer; $SmtpUsername = $settings->SmtpUserName; $SmtpPassword = $settings->SmtpPassword; $SmtpPort = $settings->SmtpPort; $email = new PHPMailer(true); // Only for development $message = "" . $message . ""; try { //Server settings $email->isSMTP(); $email->Host = (string) $SmtpHost; $email->SMTPAuth = true; $email->Username = (string) $SmtpUsername; $email->Password = (string) $SmtpPassword; $email->Port = (int) $SmtpPort; $email->SMTPSecure = $email->Port === 465 ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS; $email->Timeout = 15; $email->CharSet = 'UTF-8'; $email->isHTML(true); $email->setFrom((string) ($settings->SmtpFromEmail ?? $settings->SmtpUserName), self::APPLICATION_NAME); if (is_array($to)) { foreach ($to as $address) { $address = trim(str_replace(" ", "", $address)); if (empty($address)) { continue; } $email->addAddress($address); } } else { $email->addAddress($to); } $email->Subject = $subject; $email->Body = $message; $email->AltBody = html_entity_decode(strip_tags(preg_replace('/]*>(.*?)<\/a>/is', '$2 ($1)', $message)), ENT_QUOTES | ENT_HTML5, 'UTF-8'); if (is_array($attachments)) { foreach ($attachments as $index => $item) { if (!empty($item)) { $email->addStringAttachment($item, ($attachment_names[$index] ?? "unknown")); } } } else { if (!empty($attachments)) { $email->addStringAttachment($attachments, $attachment_names); } } $success = $email->Send(); } catch (\Throwable $e) { error_log('Email delivery failed: ' . $e->getMessage()); $success = false; } return $success; } // ============================== // Send an Error Email to Support // ============================== public function sendErrorEmail($error_message = "", $error_file = "", $error_line = "") { $settings = $this->getSettings(); $Environment = (string) $settings->Environment; $SupportEmail = (string) $settings->SupportEmail; if ($Environment == "Development") { return; } $application_name = self::APPLICATION_NAME; $message = ""; $message .= "

An application error has occurred in the {$application_name} application.

"; $message .= "

File: {$error_file}

"; $message .= "

Line: {$error_line}

"; $message .= "

Error: {$error_message}

"; $to = $SupportEmail; $subject = "{$application_name} Application Error"; $this->sendEmail($to, $subject, $message); } } ?>