93 lines
3.3 KiB
PHP
93 lines
3.3 KiB
PHP
<?php
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
class Mailer {
|
|
private static $instance = null;
|
|
private $mailer;
|
|
private $logger;
|
|
|
|
private function __construct() {
|
|
$this->mailer = new PHPMailer(true);
|
|
$this->logger = AppLogger::getInstance();
|
|
|
|
// Server settings
|
|
$this->mailer->isSMTP();
|
|
$this->mailer->Host = SMTP_HOST;
|
|
$this->mailer->SMTPAuth = true;
|
|
$this->mailer->Username = SMTP_USERNAME;
|
|
$this->mailer->Password = SMTP_PASSWORD;
|
|
$this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$this->mailer->Port = SMTP_PORT;
|
|
$this->mailer->CharSet = 'UTF-8';
|
|
|
|
// Default sender
|
|
$this->mailer->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
|
|
}
|
|
|
|
public static function getInstance() {
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function sendOrderConfirmation($order, $user) {
|
|
try {
|
|
$this->mailer->addAddress($user['email'], $user['name']);
|
|
$this->mailer->Subject = 'تأكيد الطلب - ShubraVeil';
|
|
|
|
// Load email template
|
|
$template = file_get_contents(__DIR__ . '/../templates/emails/order-confirmation.html');
|
|
|
|
// Replace placeholders
|
|
$template = str_replace('{ORDER_ID}', $order['id'], $template);
|
|
$template = str_replace('{USER_NAME}', $user['name'], $template);
|
|
$template = str_replace('{ORDER_TOTAL}', $order['total'], $template);
|
|
|
|
$this->mailer->isHTML(true);
|
|
$this->mailer->Body = $template;
|
|
|
|
$this->mailer->send();
|
|
$this->logger->info('Order confirmation email sent', ['order_id' => $order['id']]);
|
|
return true;
|
|
} catch (Exception $e) {
|
|
$this->logger->error('Failed to send order confirmation email', [
|
|
'error' => $e->getMessage(),
|
|
'order_id' => $order['id']
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function sendPasswordReset($user, $token) {
|
|
try {
|
|
$this->mailer->addAddress($user['email'], $user['name']);
|
|
$this->mailer->Subject = 'إعادة تعيين كلمة المرور - ShubraVeil';
|
|
|
|
$resetLink = SITE_URL . '/reset-password?token=' . $token;
|
|
|
|
// Load email template
|
|
$template = file_get_contents(__DIR__ . '/../templates/emails/password-reset.html');
|
|
|
|
// Replace placeholders
|
|
$template = str_replace('{USER_NAME}', $user['name'], $template);
|
|
$template = str_replace('{RESET_LINK}', $resetLink, $template);
|
|
|
|
$this->mailer->isHTML(true);
|
|
$this->mailer->Body = $template;
|
|
|
|
$this->mailer->send();
|
|
$this->logger->info('Password reset email sent', ['user_id' => $user['id']]);
|
|
return true;
|
|
} catch (Exception $e) {
|
|
$this->logger->error('Failed to send password reset email', [
|
|
'error' => $e->getMessage(),
|
|
'user_id' => $user['id']
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
}
|