diff --git a/app/Http/Controllers/Admin/DocketEntryController.php b/app/Http/Controllers/Admin/DocketEntryController.php index 97fa7520..eae885a8 100644 --- a/app/Http/Controllers/Admin/DocketEntryController.php +++ b/app/Http/Controllers/Admin/DocketEntryController.php @@ -144,39 +144,59 @@ class DocketEntryController extends Controller } /** - * Send email notifications to subscribers. - * TESTING MODE: Only sends to chris@deafgain.org + * Send email notifications to all active subscribers. */ private function sendEmailNotifications(DocketEntry $entry): void { try { - // TESTING MODE: Only send to chris@deafgain.org - // In production, this would query all active subscribers - $testEmail = 'chris@deafgain.org'; - $testToken = 'test-unsubscribe-token'; // Placeholder for testing + // Get all active subscribers + $subscribers = Subscription::where('is_active', true)->get(); + + if ($subscribers->isEmpty()) { + Log::info('No active subscribers to notify', [ + 'entry_id' => $entry->id, + ]); + return; + } - Log::info('Sending email notification for new docket entry', [ + Log::info('Sending email notifications for new docket entry', [ 'entry_id' => $entry->id, 'entry_title' => $entry->title, - 'recipient' => $testEmail, + 'subscriber_count' => $subscribers->count(), ]); - Mail::to($testEmail)->send(new NewDocketEntryNotification($entry, $testToken)); + $successCount = 0; + $failureCount = 0; - Log::info('Email notification sent successfully', [ + foreach ($subscribers as $subscriber) { + try { + Mail::to($subscriber->email)->send( + new NewDocketEntryNotification($entry, $subscriber->unsubscribe_token) + ); + $successCount++; + + Log::debug('Email sent to subscriber', [ + 'entry_id' => $entry->id, + 'subscriber_email' => $subscriber->email, + ]); + } catch (\Exception $e) { + $failureCount++; + Log::error('Failed to send email to subscriber', [ + 'entry_id' => $entry->id, + 'subscriber_email' => $subscriber->email, + 'error' => $e->getMessage(), + ]); + } + } + + Log::info('Email notification batch complete', [ 'entry_id' => $entry->id, - 'recipient' => $testEmail, + 'success_count' => $successCount, + 'failure_count' => $failureCount, + 'total_subscribers' => $subscribers->count(), ]); - - // Production code (commented out for testing): - // $subscribers = Subscription::where('is_active', true)->get(); - // foreach ($subscribers as $subscriber) { - // Mail::to($subscriber->email)->send( - // new NewDocketEntryNotification($entry, $subscriber->unsubscribe_token) - // ); - // } } catch (\Exception $e) { - Log::error('Failed to send email notification', [ + Log::error('Failed to send email notifications', [ 'entry_id' => $entry->id, 'error' => $e->getMessage(), ]);