Loheide-eu-mitarbeiterplatt.../app/Support/MailSettings.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

109 lines
4.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;
use Throwable;
/**
* E-Mail-Versand aus den Einstellungen („Administration → E-Mail-Versand“).
* Auf Shared Hosting gibt es meist nur SMTP-Zugangsdaten des Hosters – ohne
* Zugriff auf die .env. Die Werte hier haben Vorrang vor der .env.
*/
class MailSettings
{
public const MAILERS = [
'smtp' => 'SMTP-Server',
'sendmail' => 'Sendmail des Servers',
'log' => 'Nicht senden (nur ins Protokoll schreiben)',
];
public function __construct(private readonly Settings $settings) {}
public function configured(): bool
{
return $this->settings->get('mail.mailer') !== null;
}
/**
* @return array<string, mixed>
*/
public function values(): array
{
return [
'mailer' => $this->settings->get('mail.mailer', 'log'),
'host' => $this->settings->get('mail.host'),
'port' => $this->settings->get('mail.port', 587),
'encryption' => $this->settings->get('mail.encryption', 'tls'),
'username' => $this->settings->get('mail.username'),
'from_address' => $this->settings->get('mail.from_address'),
'from_name' => $this->settings->get('mail.from_name'),
];
}
/**
* @param array<string, mixed> $values
*/
public function save(array $values): void
{
$data = [
'mail.mailer' => $values['mailer'],
'mail.host' => $values['host'] ?? null,
'mail.port' => isset($values['port']) ? (int) $values['port'] : null,
'mail.encryption' => $values['encryption'] ?? 'tls',
'mail.username' => $values['username'] ?? null,
'mail.from_address' => $values['from_address'] ?? null,
'mail.from_name' => $values['from_name'] ?? null,
];
// Leeres Passwortfeld = gespeichertes behalten – aber nur, solange Server und Benutzer gleich bleiben.
// Sonst könnte, wer die Einstellungen ändern darf, das gespeicherte Passwort an einen eigenen Server schicken.
$destinationChanged = collect(['host' => 'mail.host', 'port' => 'mail.port', 'username' => 'mail.username'])
->contains(fn ($key, $field) => (string) ($values[$field] ?? '') !== (string) ($this->settings->get($key) ?? ''));
if (blank($values['password'] ?? null) && $destinationChanged && $this->settings->get('mail.password') && ($values['mailer'] ?? null) === 'smtp') {
throw ValidationException::withMessages(['data.password' => 'Server oder Benutzer wurden geändert – bitte das Passwort erneut eingeben.']);
}
if (filled($values['password'] ?? null)) {
$data['mail.password'] = Crypt::encryptString($values['password']);
}
$this->settings->setMany($data);
$this->apply();
}
public function apply(): void
{
if (! $this->configured()) {
return;
}
$values = $this->values();
try {
$password = $this->settings->get('mail.password') ? Crypt::decryptString($this->settings->get('mail.password')) : null;
} catch (Throwable) {
$password = null; // APP_KEY geändert – Passwort muss neu eingegeben werden
}
config([
'mail.default' => $values['mailer'],
'mail.mailers.smtp.host' => $values['host'],
'mail.mailers.smtp.port' => (int) $values['port'],
'mail.mailers.smtp.scheme' => $values['encryption'] === 'ssl' ? 'smtps' : 'smtp',
'mail.mailers.smtp.username' => $values['username'],
'mail.mailers.smtp.password' => $password,
'mail.mailers.smtp.timeout' => 15,
'mail.from.address' => $values['from_address'] ?: config('mail.from.address'),
'mail.from.name' => $values['from_name'] ?: app(Branding::class)->portalName(),
]);
// Bereits erzeugte Mailer mit alter Konfiguration verwerfen.
app()->forgetInstance('mail.manager');
app()->forgetInstance('mailer');
Mail::clearResolvedInstance('mail.manager');
}
}