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
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
109 lines
4.2 KiB
PHP
109 lines
4.2 KiB
PHP
<?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');
|
||
}
|
||
}
|