diff --git a/TODO.md b/TODO.md index 3d5ee62..ef5b290 100644 --- a/TODO.md +++ b/TODO.md @@ -63,6 +63,17 @@ - [ ] Verify TCP reachability from XAMXAM VM to LDAP server (port 636) - [ ] See `docs/LDAP_AUTH_PLAN.md` for full phase-by-phase plan +## SMTP notify_email fix + +- [x] Migration 006: add `notify_email` column to `smtp_settings` +- [x] `SmtpRelay::getSettings()` — include `notify_email` in SELECT + defaults +- [x] `SmtpRelay::updateSettings()` — persist `notify_email` +- [x] `SmtpRelay::getNotifyEmail()` — returns `notify_email` ?? `from_email` +- [x] `request-access.php` — use `getNotifyEmail()` instead of `from_email` for admin notifications +- [x] `actions/settings.php` — wire `smtp_notify_email` POST field +- [x] Template: add "Adresse de notification admin" field to SMTP form +- [x] `schema.sql` — updated DDL + ## SMTP credential validation - [x] Add `SmtpProbeException` with `field` property for structured error classification diff --git a/app/migrations/applied/006_smtp_notify_email.sql b/app/migrations/applied/006_smtp_notify_email.sql new file mode 100644 index 0000000..47911a4 --- /dev/null +++ b/app/migrations/applied/006_smtp_notify_email.sql @@ -0,0 +1,4 @@ +-- Migration 006: add notify_email to smtp_settings +-- notify_email is the address that receives admin notifications (access requests, etc.) +-- It is separate from from_email (the sender/no-reply address). +ALTER TABLE smtp_settings ADD COLUMN notify_email TEXT NOT NULL DEFAULT ''; diff --git a/app/public/admin/actions/settings.php b/app/public/admin/actions/settings.php index ef38f87..ebc44e8 100644 --- a/app/public/admin/actions/settings.php +++ b/app/public/admin/actions/settings.php @@ -34,12 +34,13 @@ if ($section === 'formulaire') { App::flash('success', "Types de travaux mis à jour."); } elseif ($section === 'smtp') { $smtpData = [ - 'host' => $_POST['smtp_host'] ?? '', - 'port' => $_POST['smtp_port'] ?? 587, - 'encryption' => $_POST['smtp_encryption'] ?? 'tls', - 'username' => $_POST['smtp_username'] ?? '', - 'from_email' => $_POST['smtp_from_email'] ?? '', - 'from_name' => $_POST['smtp_from_name'] ?? 'XAMXAM', + 'host' => $_POST['smtp_host'] ?? '', + 'port' => $_POST['smtp_port'] ?? 587, + 'encryption' => $_POST['smtp_encryption'] ?? 'tls', + 'username' => $_POST['smtp_username'] ?? '', + 'from_email' => $_POST['smtp_from_email'] ?? '', + 'from_name' => $_POST['smtp_from_name'] ?? 'XAMXAM', + 'notify_email' => $_POST['smtp_notify_email'] ?? '', ]; // Only update password when user actually typed something. $pwd = $_POST['smtp_password'] ?? ''; diff --git a/app/public/request-access.php b/app/public/request-access.php index 9ca88fe..2ceffd0 100644 --- a/app/public/request-access.php +++ b/app/public/request-access.php @@ -188,9 +188,9 @@ try { ); $plain = htmlToPlain($body); - $settings = SmtpRelay::getSettings($db); - if (!empty($settings['from_email'])) { - SmtpRelay::send($db, $settings['from_email'], $subject, $body, $plain); + $notifyEmail = SmtpRelay::getNotifyEmail($db); + if ($notifyEmail !== '') { + SmtpRelay::send($db, $notifyEmail, $subject, $body, $plain); } http_response_code(200); diff --git a/app/src/SmtpRelay.php b/app/src/SmtpRelay.php index 9c7a4fa..9ad4182 100644 --- a/app/src/SmtpRelay.php +++ b/app/src/SmtpRelay.php @@ -39,22 +39,34 @@ class SmtpRelay { */ public static function getSettings(Database $db): array { $stmt = $db->getPDO()->query( - "SELECT host, port, encryption, username, password, from_email, from_name + "SELECT host, port, encryption, username, password, from_email, from_name, notify_email FROM v_smtp_active LIMIT 1" ); $row = $stmt->fetch(); return $row ?: [ - 'host' => '', - 'port' => 587, - 'encryption' => 'tls', - 'username' => '', - 'password' => '', - 'from_email' => '', - 'from_name' => 'XAMXAM', + 'host' => '', + 'port' => 587, + 'encryption' => 'tls', + 'username' => '', + 'password' => '', + 'from_email' => '', + 'from_name' => 'XAMXAM', + 'notify_email' => '', ]; } + /** + * Return the address that should receive admin notification emails. + * Uses notify_email when set, falls back to from_email. + */ + public static function getNotifyEmail(Database $db): string + { + $s = self::getSettings($db); + $notify = trim($s['notify_email'] ?? ''); + return $notify !== '' ? $notify : trim($s['from_email'] ?? ''); + } + /** * Upsert SMTP settings. * @@ -71,25 +83,27 @@ class SmtpRelay { $stmt = $db->getPDO()->prepare( "UPDATE smtp_settings - SET host = :host, - port = :port, - encryption = :encryption, - username = :username, - password = :password, - from_email = :from_email, - from_name = :from_name, - updated_at = CURRENT_TIMESTAMP + SET host = :host, + port = :port, + encryption = :encryption, + username = :username, + password = :password, + from_email = :from_email, + from_name = :from_name, + notify_email = :notify_email, + updated_at = CURRENT_TIMESTAMP WHERE id = 1" ); $stmt->execute([ - ':host' => trim($merged['host']), - ':port' => $port, - ':encryption' => $encryption, - ':username' => trim($merged['username']), - ':password' => $merged['password'], - ':from_email' => trim($merged['from_email']), - ':from_name' => trim($merged['from_name']), + ':host' => trim($merged['host']), + ':port' => $port, + ':encryption' => $encryption, + ':username' => trim($merged['username']), + ':password' => $merged['password'], + ':from_email' => trim($merged['from_email']), + ':from_name' => trim($merged['from_name']), + ':notify_email' => trim($merged['notify_email'] ?? ''), ]); } diff --git a/app/storage/schema.sql b/app/storage/schema.sql index 640fd23..92fadc1 100644 --- a/app/storage/schema.sql +++ b/app/storage/schema.sql @@ -352,15 +352,16 @@ CREATE INDEX IF NOT EXISTS idx_share_links_active ON share_links(is_active); -- Singleton row — id is always 1. Credentials stored in clear for now. CREATE TABLE IF NOT EXISTS smtp_settings ( - id INTEGER PRIMARY KEY CHECK (id = 1), - host TEXT NOT NULL DEFAULT '', - port INTEGER NOT NULL DEFAULT 587, - encryption TEXT NOT NULL DEFAULT 'tls', -- 'tls' | 'ssl' | 'none' - username TEXT NOT NULL DEFAULT '', - password TEXT NOT NULL DEFAULT '', -- stored in clear for now; encrypt later - from_email TEXT NOT NULL DEFAULT '', - from_name TEXT NOT NULL DEFAULT 'XAMXAM', - updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + id INTEGER PRIMARY KEY CHECK (id = 1), + host TEXT NOT NULL DEFAULT '', + port INTEGER NOT NULL DEFAULT 587, + encryption TEXT NOT NULL DEFAULT 'tls', -- 'tls' | 'ssl' | 'none' + username TEXT NOT NULL DEFAULT '', + password TEXT NOT NULL DEFAULT '', -- stored in clear for now; encrypt later + from_email TEXT NOT NULL DEFAULT '', + from_name TEXT NOT NULL DEFAULT 'XAMXAM', + notify_email TEXT NOT NULL DEFAULT '', -- recipient for admin notifications + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ); INSERT OR IGNORE INTO smtp_settings (id) VALUES (1); diff --git a/app/templates/admin/parametres.php b/app/templates/admin/parametres.php index 228e530..52587b4 100644 --- a/app/templates/admin/parametres.php +++ b/app/templates/admin/parametres.php @@ -265,16 +265,24 @@