mail_settings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. // Αρχείο: /var/www/html/hockey-cms/admin/mail_settings.php
  3. require_once 'admin_auth.php';
  4. require_once '../config/database.php';
  5. require_once '../vendor/autoload.php';
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\Exception;
  8. if (!isset($_SESSION['user_id']) or $_SESSION['role_id'] != 1) {
  9. header("Location: ../index.php");
  10. exit;
  11. }
  12. // --- Μηχανισμός Αλλαγής Γλώσσας ---
  13. if (isset($_GET['lang']) && in_array($_GET['lang'], ['el', 'en'])) {
  14. $_SESSION['lang'] = $_GET['lang'];
  15. }
  16. $current_lang = $_SESSION['lang'] ?? 'el';
  17. $translations = [
  18. 'el' => [
  19. 'page_title' => 'Ρυθμίσεις Email (SMTP)',
  20. 'header_title' => 'Ρυθμίσεις Διακομιστή Email (SMTP)',
  21. 'provider_settings' => 'Ρυθμίσεις Παρόχου',
  22. 'gmail_instructions' => 'Οδηγίες για GMAIL: Αν χρησιμοποιείτε Gmail, βάλτε Host: smtp.gmail.com, Port: 587, Ασφάλεια: TLS. Πρέπει να ενεργοποιήσετε το 2-Factor Authentication και να δημιουργήσετε ένα App Password.',
  23. 'smtp_host' => 'Διακομιστής (SMTP Host) *',
  24. 'smtp_port' => 'Θύρα (SMTP Port) *',
  25. 'smtp_user' => 'Όνομα Χρήστη / Email (Username) *',
  26. 'smtp_pass' => 'Κωδικός Πρόσβασης (App Password) *',
  27. 'encryption' => 'Κρυπτογράφηση *',
  28. 'enc_tls' => 'STARTTLS (Συνιστάται - Port 587)',
  29. 'enc_ssl' => 'SSL (Port 465)',
  30. 'sender_details' => 'Στοιχεία Αποστολέα (Αυτό που βλέπει ο πελάτης)',
  31. 'sender_email' => 'Email Αποστολέα *',
  32. 'sender_name' => 'Όνομα Αποστολέα *',
  33. 'btn_save' => '💾 Αποθήκευση Ρυθμίσεων',
  34. 'test_title' => 'Δοκιμή Αποστολής (Test Email)',
  35. 'test_desc' => 'Αφού αποθηκεύσετε τις ρυθμίσεις σας, στείλτε ένα δοκιμαστικό email στον εαυτό σας.',
  36. 'test_email_addr' => 'Διεύθυνση Email παραλήπτη',
  37. 'btn_test' => '✉️ Αποστολή Δοκιμαστικού',
  38. 'msg_save_ok' => 'Οι ρυθμίσεις SMTP αποθηκεύτηκαν επιτυχώς!',
  39. 'msg_err_email' => 'Παρακαλώ εισάγετε ένα έγκυρο Email!',
  40. 'msg_test_ok' => 'Το δοκιμαστικό Email στάλθηκε επιτυχώς στο',
  41. 'msg_err_smtp' => 'Αποτυχία Αποστολής (SMTP Error).'
  42. ],
  43. 'en' => [
  44. 'page_title' => 'Email Settings (SMTP)',
  45. 'header_title' => 'Email Server Settings (SMTP)',
  46. 'provider_settings' => 'Provider Settings',
  47. '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.',
  48. 'smtp_host' => 'Server (SMTP Host) *',
  49. 'smtp_port' => 'Port (SMTP Port) *',
  50. 'smtp_user' => 'Username / Email *',
  51. 'smtp_pass' => 'Password (App Password) *',
  52. 'encryption' => 'Encryption *',
  53. 'enc_tls' => 'STARTTLS (Recommended - Port 587)',
  54. 'enc_ssl' => 'SSL (Port 465)',
  55. 'sender_details' => 'Sender Details (Seen by customer)',
  56. 'sender_email' => 'Sender Email *',
  57. 'sender_name' => 'Sender Name *',
  58. 'btn_save' => '💾 Save Settings',
  59. 'test_title' => 'Test Email Delivery',
  60. 'test_desc' => 'After saving your settings, send a test email to yourself.',
  61. 'test_email_addr' => 'Recipient Email Address',
  62. 'btn_test' => '✉️ Send Test Email',
  63. 'msg_save_ok' => 'SMTP settings saved successfully!',
  64. 'msg_err_email' => 'Please enter a valid Email!',
  65. 'msg_test_ok' => 'Test Email sent successfully to',
  66. 'msg_err_smtp' => 'Delivery Failed (SMTP Error).'
  67. ]
  68. ];
  69. $t = $translations[$current_lang];
  70. header('Content-Type: text/html; charset=utf-8');
  71. $message = '';
  72. $setting_keys = ['smtp_host', 'smtp_port', 'smtp_username', 'smtp_password', 'smtp_encryption', 'mail_from_address', 'mail_from_name'];
  73. // --- 1. ΑΠΟΘΗΚΕΥΣΗ ΡΥΘΜΙΣΕΩΝ ---
  74. if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and $_POST['action'] == 'save_settings') {
  75. try {
  76. $pdo->beginTransaction();
  77. foreach ($setting_keys as $key) {
  78. $value = trim($_POST[$key] ?? '');
  79. $check = $pdo->prepare("SELECT COUNT(*) FROM settings WHERE `key` = ?");
  80. $check->execute([$key]);
  81. if ($check->fetchColumn() > 0) {
  82. $stmt =$pdo->prepare("UPDATE settings SET `value` = ? WHERE `key` = ?");
  83. $stmt->execute([$value,$key]);
  84. } else {
  85. $stmt =$pdo->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?)");
  86. $stmt->execute([$key,$value]);
  87. }
  88. }
  89. $pdo->commit();$message = "<div class='alert success'>{$t['msg_save_ok']}</div>";
  90. } catch (PDOException $e) {
  91. $pdo->rollBack();$message = "<div class='alert error'>Σφάλμα: " . $e->getMessage() . "</div>";
  92. }
  93. }
  94. // --- Ανάγνωση υφιστάμενων ρυθμίσεων ---
  95. $current_settings = [];
  96. $stmt =$pdo->query("SELECT `key`, `value` FROM settings WHERE `key` IN ('" . implode("','", $setting_keys) . "')");
  97. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {$current_settings[$row['key']] =$row['value'];
  98. }
  99. // --- 2. ΑΠΟΣΤΟΛΗ ΔΟΚΙΜΑΣΤΙΚΟΥ EMAIL ---
  100. if ($_SERVER['REQUEST_METHOD'] === 'POST' and isset($_POST['action']) and$_POST['action'] == 'test_email') {
  101. $test_email = trim($_POST['test_email']);
  102. if (empty($test_email) or !filter_var($test_email, FILTER_VALIDATE_EMAIL)) {$message = "<div class='alert error'>{$t['msg_err_email']}</div>";
  103. } else {
  104. $mail = new PHPMailer(true);
  105. try {
  106. $mail->isSMTP();$mail->Host = $current_settings['smtp_host'] ?? '';$mail->SMTPAuth = true;
  107. $mail->Username =$current_settings['smtp_username'] ?? '';
  108. $mail->Password =$current_settings['smtp_password'] ?? '';
  109. $encryption =$current_settings['smtp_encryption'] ?? 'tls';
  110. if ($encryption === 'ssl') {$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  111. } else {
  112. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  113. }
  114. $mail->Port = (int)($current_settings['smtp_port'] ?? 587);$mail->CharSet = 'UTF-8';
  115. $from_email = !empty($current_settings['mail_from_address']) ?$current_settings['mail_from_address'] : 'info@yourshop.com';
  116. $from_name = !empty($current_settings['mail_from_name']) ? $current_settings['mail_from_name'] : 'Hockey CMS';$mail->setFrom($from_email,$from_name);
  117. $mail->addAddress($test_email);
  118. $mail->isHTML(true);$mail->Subject = 'Επιβεβαίωση SMTP - Hockey CMS';
  119. $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>';
  120. $mail->send();$message = "<div class='alert success'>{$t['msg_test_ok']} <strong>{$test_email}</strong>!</div>";
  121. } catch (Exception $e) {$message = "<div class='alert error'>{$t['msg_err_smtp']} <br><small>{$mail->ErrorInfo}</small></div>";
  122. }
  123. }
  124. }
  125. ?>
  126. <!DOCTYPE html>
  127. <html lang="<?php echo $current_lang; ?>">
  128. <head>
  129. <meta charset="UTF-8">
  130. <title><?php echo $t['page_title']; ?> - Hockey CMS</title>
  131. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  132. <style>
  133. :root {
  134. --primary: #f97316;
  135. --primary-hover: #ea580c;
  136. --bg-color: #0b1120;
  137. --panel-bg: #1e293b;
  138. --border-color: #334155;
  139. --text-main: #f8fafc;
  140. --text-muted: #94a3b8;
  141. }
  142. body { font-family: 'Inter', sans-serif; background-color: var(--bg-color); color: var(--text-main); margin: 0; }
  143. .content { margin-left: 250px; padding: 30px; }
  144. .content h2 { margin-top: 0; color: var(--primary); font-size: 26px; text-transform: uppercase; letter-spacing: 0.5px; }
  145. .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; }
  146. .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; }
  147. .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
  148. .form-group { margin-bottom: 20px; }
  149. .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;}
  150. .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; }
  151. .form-group input:focus, .form-group select:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.2); }
  152. .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; }
  153. .btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
  154. .btn-test { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
  155. .btn-test:hover { background: #10b981; color: white; }
  156. .alert { padding: 15px; border-radius: 6px; margin-bottom: 20px; font-weight: 600; font-size: 14px;}
  157. .success { background: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
  158. .error { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
  159. .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; }
  160. </style>
  161. </head>
  162. <body>
  163. <?php include 'sidebar.php'; ?>
  164. <div class="content">
  165. <h2><?php echo $t['header_title']; ?></h2>
  166. <?php echo $message; ?>
  167. <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 25px;">
  168. <div class="card">
  169. <h3><?php echo $t['provider_settings']; ?></h3>
  170. <div class="info-box">
  171. <?php echo $t['gmail_instructions']; ?>
  172. </div>
  173. <form method="POST" action="mail_settings.php">
  174. <input type="hidden" name="action" value="save_settings">
  175. <div class="form-grid">
  176. <div class="form-group">
  177. <label><?php echo $t['smtp_host']; ?></label>
  178. <input type="text" name="smtp_host" value="<?php echo htmlspecialchars($current_settings['smtp_host'] ?? ''); ?>" placeholder="smtp.gmail.com" required>
  179. </div>
  180. <div class="form-group">
  181. <label><?php echo $t['smtp_port']; ?></label>
  182. <input type="number" name="smtp_port" value="<?php echo htmlspecialchars($current_settings['smtp_port'] ?? '587'); ?>" placeholder="587" required>
  183. </div>
  184. <div class="form-group">
  185. <label><?php echo $t['smtp_user']; ?></label>
  186. <input type="text" name="smtp_username" value="<?php echo htmlspecialchars($current_settings['smtp_username'] ?? ''); ?>" placeholder="email@gmail.com" required>
  187. </div>
  188. <div class="form-group">
  189. <label><?php echo $t['smtp_pass']; ?></label>
  190. <input type="password" name="smtp_password" value="<?php echo htmlspecialchars($current_settings['smtp_password'] ?? ''); ?>" placeholder="••••••••••••" required>
  191. </div>
  192. <div class="form-group">
  193. <label><?php echo $t['encryption']; ?></label>
  194. <select name="smtp_encryption" required>
  195. <option value="tls" <?php echo (isset($current_settings['smtp_encryption']) and $current_settings['smtp_encryption'] == 'tls') ? 'selected' : ''; ?>><?php echo$t['enc_tls']; ?></option>
  196. <option value="ssl" <?php echo (isset($current_settings['smtp_encryption']) and $current_settings['smtp_encryption'] == 'ssl') ? 'selected' : ''; ?>><?php echo$t['enc_ssl']; ?></option>
  197. </select>
  198. </div>
  199. </div>
  200. <h3 style="margin-top: 20px;"><?php echo $t['sender_details']; ?></h3>
  201. <div class="form-grid">
  202. <div class="form-group">
  203. <label><?php echo $t['sender_email']; ?></label>
  204. <input type="email" name="mail_from_address" value="<?php echo htmlspecialchars($current_settings['mail_from_address'] ?? ''); ?>" placeholder="no-reply@domain.com" required>
  205. </div>
  206. <div class="form-group">
  207. <label><?php echo $t['sender_name']; ?></label>
  208. <input type="text" name="mail_from_name" value="<?php echo htmlspecialchars($current_settings['mail_from_name'] ?? ''); ?>" placeholder="Store Name" required>
  209. </div>
  210. </div>
  211. <button type="submit" class="btn"><?php echo $t['btn_save']; ?></button>
  212. </form>
  213. </div>
  214. <div class="card" style="height: fit-content;">
  215. <h3><?php echo $t['test_title']; ?></h3>
  216. <p style="font-size: 13px; color: var(--text-muted); margin-bottom: 20px;"><?php echo $t['test_desc']; ?></p>
  217. <form method="POST" action="mail_settings.php">
  218. <input type="hidden" name="action" value="test_email">
  219. <div class="form-group">
  220. <label><?php echo $t['test_email_addr']; ?></label>
  221. <input type="email" name="test_email" placeholder="example@email.com" required>
  222. </div>
  223. <button type="submit" class="btn btn-test"><?php echo $t['btn_test']; ?></button>
  224. </form>
  225. </div>
  226. </div>
  227. </div>
  228. </body>
  229. </html>