I want to send emails to new registered users.
Sending emails using a queue.
The job is added to the queue when the user is authenticated.
Controller:
namespace AppHttpControllers;
class LoginController extends Controller
{
public function login(Request $request)
{
$data = $request->validate([
'email' => ['required', 'email'],
'password' => ['required', 'min:5', 'max:10', 'confirmed']
]);
if (Auth::attempt($data, $request->input('remember'))) {
$request->session()->regenerate();
**SendEmail::dispatch(Auth::user());**
return redirect()->route('web.category');
}
return to_route('login')->withErrors(['fail-login' => 'error']);
}
AppJobsSendEmail class:
namespace AppJobs;
class SendEmail implements ShouldQueue
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function handle(): void
{
**event(new Registered($this->user)); **
}
As a result, emails are not sent because an unauthenticated user is passed to the Jobs/SendEmail class.
How did I check it?
The handle method of the SendEmail class dispatches the** Registered event** to the SendEmailVerificationNotification listener.
Next, the listener calls its own handle method in which it checks the user and if the check is passed, the letter is sent to the user.
And here are the test conditions if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()).
The user does not pass this test.
What’s my mistake?