Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions app/Services/UserEmailChangeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/UserEmailChangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
Expand Down Expand Up @@ -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
Expand Down
Loading