Testbetrieb: eine Schranke fuer jede ausgehende E-Mail
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m10s

EnforceMailTestMode prueft jede Mail unmittelbar vor dem Versand (MessageSending,
jeder Mailer): im Testbetrieb gehen An/Cc/Bcc nur an die Testadresse, ohne
Testadresse wird nichts versendet; der Modus wird je Mail frisch gelesen.
Ersetzt alwaysTo am Standard-Mailer. Tests inkl. Pruefung, dass kein Code am
Mailer vorbei versendet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 5.5 committed 2026-11-01 21:03:24 +01:00
1 parent ac9d20b812
commit 0c10880e4f
6 files changed
+248 -9

No files matched your search

+2
View File
@@ -25,6 +25,8 @@ public function handle(): int
$this->line("Versandweg: {$mailer}".($mailer === 'smtp' ? ' ('.config('mail.mailers.smtp.host').':'.config('mail.mailers.smtp.port').')' : '').' · Absender: '.config('mail.from.address'));
if (filled($redirect)) {
$this->line("Umleitung aktiv: die Mail geht an {$redirect} statt an {$to}.");
} elseif (! Settings::isLive()) {
$this->warn('Testbetrieb ohne Testadresse: Es wird keine E-Mail versendet. Testempfänger unter System › Betrieb eintragen.');
}
if ($mailer === 'log') {
$this->warn('Versandweg "log": es wird nichts zugestellt. Zum echten Versand MAIL_MAILER=smtp und MAIL_PASSWORD setzen (docs/BETRIEB.md §5a).');
+67
View File
@@ -0,0 +1,67 @@
<?php
namespace App\Listeners;
use App\Models\Tenant;
use App\Support\Settings;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
/**
* The one gate every outgoing e-mail passes – whichever mailer, mailable or notification sent it (Laravel fires
* MessageSending for every message right before the transport). In test operation it replaces every recipient (To, Cc,
* Bcc) by the test recipient; when no test recipient can be determined, the mail is not sent at all. Only a landlord
* switched live (and no server override) lets mails reach their real recipients.
*
* The operating mode is read fresh from the database for every mail, so a switch in the admin applies at once, also in
* long-running processes. Without any landlord the installation counts as being in test operation.
*/
class EnforceMailTestMode
{
public const ORIGINAL_RECIPIENTS_HEADER = 'X-Fewo-Testbetrieb-Original';
/**
* @return false|null false stops the mail; null lets it go on
*/
public function handle(MessageSending $event): ?bool
{
$message = $event->message;
$tenant = Settings::tenant();
$tenant = $tenant instanceof Tenant ? ($tenant->fresh() ?? $tenant) : null;
$override = config('fewo.mail_redirect_to');
if (blank($override) && $tenant?->isLive()) {
return null;
}
$redirect = Settings::mailRedirectFor($tenant);
if (blank($redirect) || filter_var($redirect, FILTER_VALIDATE_EMAIL) === false) {
// Test operation without anybody to redirect to: never fall through to the real recipients.
Log::warning('E-Mail im Testbetrieb nicht versendet: keine Testadresse eingerichtet.', ['subject' => $message instanceof Email ? $message->getSubject() : null, 'tenant' => $tenant?->id]);
return false;
}
if (! $message instanceof Email) {
Log::warning('E-Mail im Testbetrieb nicht versendet: unbekannter Nachrichtentyp.', ['type' => $message::class]);
return false;
}
$original = collect([...$message->getTo(), ...$message->getCc(), ...$message->getBcc()])
->map(fn (Address $address): string => $address->getAddress())->unique()->implode(', ');
$headers = $message->getHeaders();
$headers->remove('Cc');
$headers->remove('Bcc');
$headers->remove(self::ORIGINAL_RECIPIENTS_HEADER);
$message->to($redirect);
if ($original !== '') {
$headers->addTextHeader(self::ORIGINAL_RECIPIENTS_HEADER, mb_substr($original, 0, 900));
}
return null;
}
}
+3 -7
View File
@@ -2,8 +2,8 @@
namespace App\Tenancy;
use App\Listeners\EnforceMailTestMode;
use App\Models\Tenant;
use App\Support\Settings;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\Mail;
use Throwable;
@@ -146,7 +146,8 @@ public function apply(?Tenant $tenant): void
}
/**
* The mailer sends everything to one address while the landlord is in test mode.
* Rebuilds the mailers so that they use the sender of the current landlord. Where mails go in test operation is
* decided for every single mail by the one gate {@see EnforceMailTestMode}, not here.
*/
public function applyMailRouting(?Tenant $tenant): void
{
@@ -157,11 +158,6 @@ public function applyMailRouting(?Tenant $tenant): void
}
$manager->purge();
$redirect = Settings::mailRedirectFor($tenant);
if (filled($redirect)) {
$manager->alwaysTo($redirect);
}
}
/**