Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

obs's avatar
Level 3

Trigger Email Verification when user was created programmatically (via API)

I have the possibility to create a user via the api and have the laravel email verification (https://laravel.com/docs/7.x/verification) in place and working.

When a user registered on the website, a link is sent and everything works, but when the user is created programmatically, no link is sent to verify the email. How can I trigger this programmatically?

0 likes
9 replies
bugsysha's avatar

You have three common options:

  1. Use model observer https://laravel.com/docs/7.x/eloquent#observers
  2. Use $dispatchesEvents on model
  3. Create command for creating users and send email from there

Just make sure that it will not send an additional email.

Once you decide which option you want I can expand if you still need help.

obs's avatar
Level 3

Thanks for listing the options, I already have the code for creating the user is place, so maybe option 3 would be the simplest for me? It would be great if you could expand on this.

MichalOravec's avatar

@obs When you create an user via API you have to trigger Registered event.

You can see it here, this is trait used in normal registration

https://github.com/laravel/ui/blob/2.x/auth-backend/RegistersUsers.php#L34

From docs: https://laravel.com/docs/7.x/verification#model-preparation

Once this interface has been added to your model, newly registered users will automatically be sent an email containing an email verification link. As you can see by examining your EventServiceProvider, Laravel already contains a SendEmailVerificationNotification listener that is attached to the Illuminate\Auth\Events\Registered event.

use Illuminate\Auth\Events\Registered;

// $user variable is your created user

event(new Registered($user));
obs's avatar
Level 3

Thanks for your reply!

Unfortunately no email is sent. (At least I haven't received one. Is there a way to check if the email was sent?) Here is my code

public function registerUser(Request $request){
        $data = $request->all();

        $validator = Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6'
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $user = \App\User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        event( new Registered( $user ) );


        $success['message'] = "Registration successfull. Please verify your Email address.";
        $success['user'] = $userData = new UserResource( $user );


        return response()->json(['success' => $success], $this->successStatus);

    }

I also have added the use statement.

Any idea what could be the reason for this not to work?

bugsysha's avatar

I would also go with option 3, just didn't want to force you into it.

Have you ever created a command? There are tutorials on how to do it so you can check them and if you need any help you can post a question when you get stuck.

obs's avatar
Level 3

I didn't get that "command" is something Laravel specific. I will look into that. But I guess I still need to know how to trigger the email verification link to be sent. That will be part of the command, right?

obs's avatar
Level 3

Thanks, the listener was missing.

Please or to participate in this conversation.