with('license') ->whereNull('delivered_at') ->whereNotNull('recipient_email') ->where('recipient_email', '!=', '') ->oldest() ->limit(max(1, min($limit, 100))) ->get(); foreach ($pending as $outbox) { $license = $outbox->license; if (! $license) { continue; } $plain = Crypt::decryptString($outbox->encrypted_license_key); $recipient = strtolower(trim((string) $outbox->recipient_email)); if ($recipient === '') { continue; } $data = [ 'customerName' => $license->customer_name, 'edition' => $license->edition, 'licenseKey' => $plain, 'expiresAt' => $license->expires_at, 'portalUrl' => config('sonardesk.portal_url') ?: url('/portal'), 'downloadUrl' => route('release'), ]; Mail::send(['html' => 'licensing::emails.license-key', 'text' => 'licensing::emails.license-key-text'], $data, function ($message) use ($recipient): void { $message->to($recipient)->subject('Your SonarDesk licence key'); }); $outbox->update(['delivered_at' => now()]); $delivered++; } return $delivered; } }