| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- // Αρχείο: /var/www/html/hockey-cms/admin/mail_settings.php
- require_once 'admin_auth.php';
- require_once '../config/database.php';
- require_once '../vendor/autoload.php';
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
- if (!isset($_SESSION['user_id']) or $_SESSION['role_id'] != 1) {
- header("Location: ../index.php");
- exit;
- }
- // --- Μηχανισμός Αλλαγής Γλώσσας ---
- if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
- $_SESSION['lang'] = $_GET['lang'];
- }
- $current_lang = $_SESSION['lang'] ?? 'el';
- $translations = [
- 'el' => [
- 'page_title' => 'Ρυθμίσεις Email (SMTP)',
- 'header_title' => 'Ρυθμίσεις Διακομιστή Email (SMTP)',
- 'provider_settings' => 'Ρυθμίσεις Παρόχου',
- 'gmail_instructions' => 'Οδηγίες για GMAIL: Αν χρησιμοποιείτε Gmail, βάλτε Host: smtp.gmail.com, Port: 587, Ασφάλεια: TLS. Πρέπει να ενεργοποιήσετε το 2-Factor Authentication και να δημιουργήσετε ένα App Password.',
- 'smtp_host' => 'Διακομιστής (SMTP Host) *',
- 'smtp_port' => 'Θύρα (SMTP Port) *',
- 'smtp_user' => 'Όνομα Χρήστη / Email (Username) *',
- 'smtp_pass' => 'Κωδικός Πρόσβασης (App Password) *',
- 'encryption' => 'Κρυπτογράφηση *',
- 'enc_tls' => 'STARTTLS (Συνιστάται - Port 587)',
- 'enc_ssl' => 'SSL (Port 465)',
- 'sender_details' => 'Στοιχεία Αποστολέα (Αυτό που βλέπει ο πελάτης)',
- 'sender_email' => 'Email Αποστολέα *',
- 'sender_name' => 'Όνομα Αποστολέα *',
- 'btn_save' => '💾 Αποθήκευση Ρυθμίσεων',
- 'test_title' => 'Δοκιμή Αποστολής (Test Email)',
- 'test_desc' => 'Αφού αποθηκεύσετε τις ρυθμίσεις σας, στείλτε ένα δοκιμαστικό email στον εαυτό σας.',
- 'test_email_addr' => 'Διεύθυνση Email παραλήπτη',
- 'btn_test' => '✉️ Αποστολή Δοκιμαστικού',
- 'msg_save_ok' => 'Οι ρυθμίσεις SMTP αποθηκεύτηκαν επιτυχώς!',
- 'msg_err_email' => 'Παρακαλώ εισάγετε ένα έγκυρο Email!',
- 'msg_test_ok' => 'Το δοκιμαστικό Email στάλθηκε επιτυχώς στο',
- 'msg_err_smtp' => 'Αποτυχία Αποστολής (SMTP Error).'
- ],
- 'en' => [
- 'page_title' => 'Email Settings (SMTP)',
- 'header_title' => 'Email Server Settings (SMTP)',
- 'provider_settings' => 'Provider Settings',
- 'gmail_instructions' => 'GMAIL Instructions: If using Gmail, set Host: smtp.gmail.com, Port: 587, Security: TLS. You must enable 2-Factor Auth and create an App Password.',
- 'smtp_host' => 'Server (SMTP Host) *',
- 'smtp_port' => 'Port (SMTP Port) *',
- 'smtp_user' => 'Username / Email *',
- 'smtp_pass' => 'Password (App Password) *',
- 'encryption' => 'Encryption *',
- 'enc_tls' => 'STARTTLS (Recommended - Port 587)',
- 'enc_ssl' => 'SSL (Port 465)',
- 'sender_details' => 'Sender Details (Seen by customer)',
- 'sender_email' => 'Sender Email *',
- 'sender_name' => 'Sender Name *',
- 'btn_save' => '💾 Save Settings',
- 'test_title' => 'Test Email Delivery',
- 'test_desc' => 'After saving your settings, send a test email to yourself.',
- 'test_email_addr' => 'Recipient Email Address',
- 'btn_test' => '✉️ Send Test Email',
- 'msg_save_ok' => 'SMTP settings saved successfully!',
- 'msg_err_email' => 'Please enter a valid Email!',
- 'msg_test_ok' => 'Test Email sent successfully to',
- 'msg_err_smtp' => 'Delivery Failed (SMTP Error).'
- ]
- ];
- $t = $translations[$current_lang];
- header('Content-Type: text/html; charset=utf-8');
- $message = '';
- $setting_keys = ['smtp_host', 'smtp_port', 'smtp_username', 'smtp_password', 'smtp_encryption', 'mail_from_address', 'mail_from_name'];
- // --- 1. ΑΠΟΘΗΚΕΥΣΗ ΡΥΘΜΙΣΕΩΝ ---
- if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and $_POST['action'] == 'save_settings') {
- try {
- $pdo->beginTransaction();
- foreach ($setting_keys as $key) {
- $value = trim($_POST[$key] ?? '');
- $check = $pdo->prepare("SELECT COUNT(*) FROM settings WHERE `key` = ?");
- $check->execute([$key]);
- if ($check->fetchColumn() > 0) {
- $stmt =$pdo->prepare("UPDATE settings SET `value` = ? WHERE `key` = ?");
- $stmt->execute([$value,$key]);
- } else {
- $stmt =$pdo->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?)");
- $stmt->execute([$key,$value]);
- }
- }
- $pdo->commit();$message = "<div class='alert success'>{$t['msg_save_ok']}</div>";
- } catch (PDOException $e) {
- $pdo->rollBack();$message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
- }
- }
- // --- Ανάγνωση υφιστάμενων ρυθμίσεων ---
- $current_settings = [];
- $stmt =$pdo->query("SELECT `key`, `value` FROM settings WHERE `key` IN ('" . implode("','", $setting_keys) . "')");
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {$current_settings[$row['key']] =$row['value'];
- }
- // --- 2. ΑΠΟΣΤΟΛΗ ΔΟΚΙΜΑΣΤΙΚΟΥ EMAIL ---
- if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and$_POST['action'] == 'test_email') {
- $test_email = trim($_POST['test_email']);
-
- if (empty($test_email) or !filter_var($test_email, FILTER_VALIDATE_EMAIL)) {$message = "<div class='alert error'>{$t['msg_err_email']}</div>";
- } else {
- $mail = new PHPMailer(true);
- try {
- $mail->isSMTP();$mail->Host = $current_settings['smtp_host'] ?? '';$mail->SMTPAuth = true;
- $mail->Username =$current_settings['smtp_username'] ?? '';
- $mail->Password =$current_settings['smtp_password'] ?? '';
-
- $encryption =$current_settings['smtp_encryption'] ?? 'tls';
- if ($encryption === 'ssl') {$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
- } else {
- $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
- }
-
- $mail->Port = (int)($current_settings['smtp_port'] ?? 587);$mail->CharSet = 'UTF-8';
- $from_email = !empty($current_settings['mail_from_address']) ?$current_settings['mail_from_address'] : 'info@yourshop.com';
- $from_name = !empty($current_settings['mail_from_name']) ? $current_settings['mail_from_name'] : 'Hockey CMS';$mail->setFrom($from_email,$from_name);
- $mail->addAddress($test_email);
- $mail->isHTML(true);$mail->Subject = 'Επιβεβαίωση SMTP - Hockey CMS';
- $mail->Body = '<div style="font-family: sans-serif; padding: 20px; background: #f8fafc; border-radius: 8px;"><h2 style="color: #2563eb;">Συγχαρητήρια! 🎉</h2><p>Αν διαβάζετε αυτό το μήνυμα, οι ρυθμίσεις του διακομιστή SMTP έχουν γίνει σωστά στο <strong>Hockey CMS</strong>.</p></div>';
- $mail->send();$message = "<div class='alert success'>{$t['msg_test_ok']} <strong>{$test_email}</strong>!</div>";
- } catch (Exception $e) {$message = "<div class='alert error'>{$t['msg_err_smtp']} <br><small>{$mail->ErrorInfo}</small></div>";
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="<?php echo $current_lang; ?>">
- <head>
- <meta charset="UTF-8">
- <title><?php echo $t['page_title']; ?> - Hockey CMS</title>
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
- <style>
- :root {
- --primary: #f97316;
- --primary-hover: #ea580c;
- --bg-color: #0b1120;
- --panel-bg: #1e293b;
- --border-color: #334155;
- --text-main: #f8fafc;
- --text-muted: #94a3b8;
- }
- body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
- .content { margin-left: 250px; padding: 30px; }
- .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
-
- .card { background: var(--panel-bg); padding: 25px; border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.3); border: 1px solid var(--border-color); margin-bottom: 25px; }
- .card h3 { margin-top: 0; color: var(--text-main); border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 15px; font-size: 18px; text-transform: uppercase; letter-spacing: 0.5px; }
-
- .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
- .form-group { margin-bottom: 20px; }
- .form-group label { display: block; margin-bottom: 8px; font-weight: 700; font-size: 13px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px;}
- .form-group input, .form-group select { width: 100%; padding: 12px; background: #0f172a; border: 1px solid var(--border-color); color: var(--text-main); border-radius: 6px; box-sizing: border-box; font-family: 'Inter', sans-serif; outline: none; transition: 0.2s; }
- .form-group input:focus, .form-group select:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.2); }
-
- .btn { padding: 12px 20px; background: var(--primary); color: white; border: none; border-radius: 6px; cursor: pointer; font-weight: 800; font-size: 14px; text-transform: uppercase; transition: 0.2s; }
- .btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
- .btn-test { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
- .btn-test:hover { background: #10b981; color: white; }
-
- .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
- .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
- .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
-
- .info-box { background: rgba(59, 130, 246, 0.1); border-left: 4px solid #3b82f6; padding: 15px; border-radius: 4px; font-size: 13px; color: #60a5fa; margin-bottom: 20px; line-height: 1.5; }
- </style>
- </head>
- <body>
-
- <?php include 'sidebar.php'; ?>
- <div class="content">
- <h2><?php echo $t['header_title']; ?></h2>
- <?php echo $message; ?>
- <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 25px;">
-
- <div class="card">
- <h3><?php echo $t['provider_settings']; ?></h3>
-
- <div class="info-box">
- <?php echo $t['gmail_instructions']; ?>
- </div>
- <form method="POST" action="mail_settings.php">
- <input type="hidden" name="action" value="save_settings">
-
- <div class="form-grid">
- <div class="form-group">
- <label><?php echo $t['smtp_host']; ?></label>
- <input type="text" name="smtp_host" value="<?php echo htmlspecialchars($current_settings['smtp_host'] ?? ''); ?>" placeholder="smtp.gmail.com" required>
- </div>
-
- <div class="form-group">
- <label><?php echo $t['smtp_port']; ?></label>
- <input type="number" name="smtp_port" value="<?php echo htmlspecialchars($current_settings['smtp_port'] ?? '587'); ?>" placeholder="587" required>
- </div>
- <div class="form-group">
- <label><?php echo $t['smtp_user']; ?></label>
- <input type="text" name="smtp_username" value="<?php echo htmlspecialchars($current_settings['smtp_username'] ?? ''); ?>" placeholder="email@gmail.com" required>
- </div>
- <div class="form-group">
- <label><?php echo $t['smtp_pass']; ?></label>
- <input type="password" name="smtp_password" value="<?php echo htmlspecialchars($current_settings['smtp_password'] ?? ''); ?>" placeholder="••••••••••••" required>
- </div>
- <div class="form-group">
- <label><?php echo $t['encryption']; ?></label>
- <select name="smtp_encryption" required>
- <option value="tls" <?php echo (isset($current_settings['smtp_encryption']) and $current_settings['smtp_encryption'] == 'tls') ? 'selected' : ''; ?>><?php echo$t['enc_tls']; ?></option>
- <option value="ssl" <?php echo (isset($current_settings['smtp_encryption']) and $current_settings['smtp_encryption'] == 'ssl') ? 'selected' : ''; ?>><?php echo$t['enc_ssl']; ?></option>
- </select>
- </div>
- </div>
- <h3 style="margin-top: 20px;"><?php echo $t['sender_details']; ?></h3>
- <div class="form-grid">
- <div class="form-group">
- <label><?php echo $t['sender_email']; ?></label>
- <input type="email" name="mail_from_address" value="<?php echo htmlspecialchars($current_settings['mail_from_address'] ?? ''); ?>" placeholder="no-reply@domain.com" required>
- </div>
- <div class="form-group">
- <label><?php echo $t['sender_name']; ?></label>
- <input type="text" name="mail_from_name" value="<?php echo htmlspecialchars($current_settings['mail_from_name'] ?? ''); ?>" placeholder="Store Name" required>
- </div>
- </div>
-
- <button type="submit" class="btn"><?php echo $t['btn_save']; ?></button>
- </form>
- </div>
- <div class="card" style="height: fit-content;">
- <h3><?php echo $t['test_title']; ?></h3>
- <p style="font-size: 13px; color: var(--text-muted); margin-bottom: 20px;"><?php echo $t['test_desc']; ?></p>
-
- <form method="POST" action="mail_settings.php">
- <input type="hidden" name="action" value="test_email">
- <div class="form-group">
- <label><?php echo $t['test_email_addr']; ?></label>
- <input type="email" name="test_email" placeholder="example@email.com" required>
- </div>
- <button type="submit" class="btn btn-test"><?php echo $t['btn_test']; ?></button>
- </form>
- </div>
-
- </div>
- </div>
- </body>
- </html>
|