Files

162 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2026-10-18 17:01:36 +00:00
<?php
// ===========
// Email Class
// ===========
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Email extends Base {
2026-10-18 19:27:17 -04:00
public function sendPasswordReset(array $user, string $url): bool {
$xml = new SimpleXMLElement('<users><record/></users>');
$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);
}
2026-10-18 17:01:36 +00:00
// 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
// =============================
2026-10-18 19:27:17 -04:00
public function emailNewUserCredentials($user, $password = null) {
$this->emailUserPasswordResetLink($user);
2026-10-18 17:01:36 +00:00
}
2026-10-18 19:27:17 -04:00
public function emailUserPasswordResetLink($user, $password = null) {
if ($user instanceof SimpleXMLElement) {
(new AuthService())->requestReset((string) $user->record->user_email);
2026-10-18 17:01:36 +00:00
}
}
// =======================================
// 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 = "<span style='font-family: Calibri, Arial, sans-serif;'>" . $message . "</span>";
try {
//Server settings
$email->isSMTP();
2026-10-18 19:27:17 -04:00
$email->Host = (string) $SmtpHost;
2026-10-18 17:01:36 +00:00
$email->SMTPAuth = true;
2026-10-18 19:27:17 -04:00
$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';
2026-10-18 17:01:36 +00:00
$email->isHTML(true);
2026-10-18 19:27:17 -04:00
$email->setFrom((string) ($settings->SmtpFromEmail ?? $settings->SmtpUserName), self::APPLICATION_NAME);
2026-10-18 17:01:36 +00:00
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;
2026-10-18 19:27:17 -04:00
$email->AltBody = html_entity_decode(strip_tags(preg_replace('/<a\s+href="([^"]*)"[^>]*>(.*?)<\/a>/is', '$2 ($1)', $message)), ENT_QUOTES | ENT_HTML5, 'UTF-8');
2026-10-18 17:01:36 +00:00
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();
2026-10-18 19:27:17 -04:00
} catch (\Throwable $e) {
2026-10-18 17:01:36 +00:00
2026-10-18 19:27:17 -04:00
error_log('Email delivery failed: ' . $e->getMessage());
2026-10-18 17:01:36 +00:00
$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 .= "<p>An application error has occurred in the {$application_name} application.<br/><br/></p>";
$message .= "<p>File: {$error_file}</p>";
$message .= "<p>Line: {$error_line}</p>";
$message .= "<p>Error: {$error_message}</p>";
$to = $SupportEmail;
$subject = "{$application_name} Application Error";
$this->sendEmail($to, $subject, $message);
}
}
?>