presseportale/app/Console/Commands/SendGoLiveMails.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

106 lines
3.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Mail\GoLivePasswordReset;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Password;
/**
* Sendet Go-Live-Passwort-Reset-Mails an alle aktiven User.
*
* Verwendung:
* php artisan auth:send-go-live-mails --dry-run
* php artisan auth:send-go-live-mails --portal=presseecho
* php artisan auth:send-go-live-mails --limit=10
* php artisan auth:send-go-live-mails --force
*/
class SendGoLiveMails extends Command
{
protected $signature = 'auth:send-go-live-mails
{--dry-run : Nur Anzahl ausgeben, keine Mails senden}
{--portal= : Nur User eines Portals (presseecho|businessportal24|both)}
{--limit=0 : Maximale Anzahl (0 = alle)}
{--force : Bestätigung überspringen}';
protected $description = 'Sendet Go-Live-Passwort-Reset-Mails an alle aktiven Benutzer.';
private const EXPIRES_IN_MINUTES = 60;
public function handle(): int
{
$isDryRun = $this->option('dry-run');
$portal = $this->option('portal');
$limit = (int) $this->option('limit');
$query = User::query()
->where('is_active', true)
->whereNotNull('email')
->when($portal, fn ($q) => $q->where('portal', $portal));
$total = $query->count();
$this->info("Aktive Benutzer gefunden: {$total}");
if ($portal) {
$this->line(" → Portal-Filter: {$portal}");
}
if ($limit > 0) {
$this->line(" → Limit: {$limit}");
$query->limit($limit);
$total = min($total, $limit);
}
if ($isDryRun) {
$this->warn("[DRY-RUN] Es würden {$total} Mails versendet. Kein tatsächlicher Versand.");
return self::SUCCESS;
}
if (! $this->option('force') && ! $this->confirm("Jetzt {$total} Go-Live-Mails senden?")) {
$this->info('Abgebrochen.');
return self::SUCCESS;
}
$sent = 0;
$failed = 0;
$bar = $this->output->createProgressBar($total);
$bar->start();
$query->each(function (User $user) use (&$sent, &$failed, $bar): void {
try {
$token = Password::broker()->createToken($user);
$resetUrl = url(route('password.reset', [
'token' => $token,
'email' => $user->email,
], false));
Mail::to($user->email)->send(
new GoLivePasswordReset($user, $resetUrl, self::EXPIRES_IN_MINUTES)
);
$sent++;
} catch (\Throwable $e) {
$failed++;
$this->newLine();
$this->error("Fehler für {$user->email}: {$e->getMessage()}");
}
$bar->advance();
});
$bar->finish();
$this->newLine(2);
$this->info("Versendet: {$sent}");
if ($failed > 0) {
$this->warn("Fehlgeschlagen: {$failed}");
}
return $failed > 0 ? self::FAILURE : self::SUCCESS;
}
}