Loheide-eu-mitarbeiterplatt.../app/Support/MailSettings.php
Friederich Loheide 11123fecc0
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m11s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m7s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m52s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 3m2s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m31s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m27s
CI / Build-Artefakte aktuell (pull_request) Successful in 53s
Formulare, WordPress-Import und E-Mail-Versand
- Formular-Plugin: Baukasten mit 13 Feldtypen, Bedingungen, Vorbelegung
  aus dem Profil, Datei-Uploads (geschützt), anonyme Formulare, „nur einmal“,
  Einsendeschluss, Sichtbarkeit, Löschfrist; Einsendungen im Backend und per
  Mail (optional ohne Angaben, Kopie an die Person); Rechte je Formular und
  Rolle für Ansehen/Export und Löschen; CSV für Excel; Pagebuilder-Block
- WordPress-Import (Plugin): WXR-Datei einlesen, Kategorien/Personen zuordnen,
  Beiträge mit Status, Datum, Beitragsbild und Bildern im Text übernehmen,
  optional Seiten mit Hierarchie; in Portionen, wiederholbar ohne Doppelungen
- E-Mail-Versand im Admin (SMTP, verschlüsseltes Passwort, Test-Mail)
- Speichertest unabhängig vom Verbrauch der Test-Suite

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 09:20:54 +00:00

100 lines
3.4 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 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.
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');
}
}