Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.7 - How can I dynamically trigger a user verification email?

Tags:

php

laravel

When I register using the registration page, the link in the email that I get works fine.

When I instead use $user->sendEmailVerificationNotification() like I saw in vendor\laravel\framework\src\Illuminate\Auth\Listeners\SendEmailVerificationNotification.php, it does not work.

I get the email, but when I click on the verify link, I get redirected to the login page and the user does not get verified.

Use Case: My system has a superuser, who can add users, and I am hoping to fire that email for the users to verify themselves.

Any help would be appreciated.

like image 862
jabdefiant Avatar asked Feb 11 '19 23:02

jabdefiant


People also ask

How to implement email verification in Laravel?

To use the email verification process, we need to implement this contract. Go to the routes >> web.php file and add the extra parameter inside Auth::routes (). This enables the new Verification controller with the route actions. You can see the new controller called VerificationController. php file already comes with Laravel 5.7.

How to verify the ownership of an account in Laravel?

Nowadays most of the modern applications force their users to verify the ownership by sending an activation email after they complete the account registration. Moving ahead, with the release of Laravel 5.7 the user email verification is shipping with the framework out of the box.

What is mustverifyemail in Laravel?

It is new in Laravel 5.7 as the verification functionality is implemented in this version. Implement mustVerify interface in the User model. In the User.php model, you can see one more contract added called MustVerifyEmail. To use the email verification process, we need to implement this contract.

How to setup SMTP credential in Laravel?

After successfully download laravel 5.7 Application, Go to your project .env file and set up database credential and move next step : After successfully set database credential, Now setup smtp credential in .env. We have used mailtrap smtp credential in this example.


1 Answers

This answer assumes you've already setup Laravel's Authentication and Email Verification.

When a user registers on the application, the controller by default is using the Illuminate\Foundation\Auth\RegistersUser trait. Notice the additional functionality that takes place in the register() method there, and also notice that a \Illuminate\Auth\Events\Registered event is generated which in turns triggers the listener SendEmailVerificationNotification (where you are currently getting your code from.)

For your custom class, you could potentially reuse the Illuminate\Foundation\Auth\RegistersUser trait on it, but that could feel strange as your class is probably not a controller and the trait involves extra controller-specific logic.

Instead, you could try ripping some code from Illuminate\Foundation\Auth\RegistersUser::register() and use it within your new class.

So, something similar to:

// If you do not yet have a new user object.
$user = \App\User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => \Illuminate\Support\Facades\Hash::make($data['password']),
]);

// Fire the event now with the user you want to receive an email.
event(new \Illuminate\Auth\Events\Registered($user));

More information:

  • https://laravel.com/docs/5.7/events
like image 200
1000Nettles Avatar answered Oct 17 '22 18:10

1000Nettles