diff --git a/app/Services/UserEmailChangeService.php b/app/Services/UserEmailChangeService.php index 04fd0ff3a..b7b2f9a62 100644 --- a/app/Services/UserEmailChangeService.php +++ b/app/Services/UserEmailChangeService.php @@ -9,6 +9,7 @@ use App\User; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\URL; use Illuminate\Support\Str; @@ -106,7 +107,7 @@ private function dispatchConfirmationEmail(User $user, string $newEmail, string ['user' => $user->id, 'token' => $token], ); - Mail::to($newEmail)->queue(new PendingEmailChangeConfirmation($user, $confirmUrl)); + $this->deliverNow($newEmail, new PendingEmailChangeConfirmation($user, $confirmUrl), 'confirmation'); } private function sendChangeNotification(User $user, string $newEmail, ?string $currentEmail): void @@ -115,7 +116,25 @@ private function sendChangeNotification(User $user, string $newEmail, ?string $c return; } - Mail::to($currentEmail)->queue(new PendingEmailChangeNotification($user, $newEmail)); + $this->deliverNow($currentEmail, new PendingEmailChangeNotification($user, $newEmail), 'notification'); + } + + private function deliverNow(string $email, PendingEmailChangeConfirmation|PendingEmailChangeNotification $mailable, string $type): void + { + try { + Mail::to($email)->send($mailable); + } catch (\Throwable $exception) { + Log::error('Failed to send login email change message.', [ + 'type' => $type, + 'recipient' => $email, + 'user_id' => $mailable->user->id ?? null, + 'message' => $exception->getMessage(), + ]); + + throw ValidationException::withMessages([ + $type === 'confirmation' ? 'new_email' : 'email' => 'We could not send the email right now. Please try again in a few minutes or contact info@codeweek.eu.', + ]); + } } public function cancelPending(User $user): void diff --git a/tests/Feature/UserEmailChangeTest.php b/tests/Feature/UserEmailChangeTest.php index 87025b1f6..32a7d5206 100644 --- a/tests/Feature/UserEmailChangeTest.php +++ b/tests/Feature/UserEmailChangeTest.php @@ -47,11 +47,11 @@ public function test_user_can_request_email_change_from_profile(): void $user->refresh(); $this->assertSame('new@example.com', $user->pending_email); - Mail::assertQueued(PendingEmailChangeConfirmation::class, function ($mail) { + Mail::assertSent(PendingEmailChangeConfirmation::class, function ($mail) { return $mail->hasTo('new@example.com'); }); - Mail::assertQueued(PendingEmailChangeNotification::class, function ($mail) { + Mail::assertSent(PendingEmailChangeNotification::class, function ($mail) { return $mail->hasTo('old@example.com'); }); } @@ -131,11 +131,11 @@ public function test_user_can_resend_pending_confirmation_email(): void $response->assertRedirect(); $response->assertSessionHas('email_change_status'); - Mail::assertQueued(PendingEmailChangeConfirmation::class, function ($mail) { + Mail::assertSent(PendingEmailChangeConfirmation::class, function ($mail) { return $mail->hasTo('new@example.com'); }); - Mail::assertNotQueued(PendingEmailChangeNotification::class); + Mail::assertNotSent(PendingEmailChangeNotification::class); } public function test_profile_update_still_cannot_change_login_email_directly(): void