3

I'm trying to get Lavarel to send a verification email when you register through the API. Is this possible?

I've added the MustVerifyEmail implement to the User class and added Auth::routes(['verify' => true]); to almost the top of my api.php file (Where my routes are). Also the SMTP server is configured correctly.

I think I might be doing something wrong. Hope to hear from you :)

P.S. I've started using Laravel for the first time today. So it might just be a really simple mistake.

Kevin Walter
  • 6,507
  • 8
  • 28
  • 32
  • Possible duplicate of [Laravel Email Verification 5.7 using REST API](https://stackoverflow.com/questions/52362927/laravel-email-verification-5-7-using-rest-api) – Ricardo Santos Dec 12 '18 at 14:56
  • It is not. I already found that post and it's about a rework for the verification email. I just want the verification email send that is already present in the system. – Kevin Walter Dec 12 '18 at 14:58

1 Answers1

2

If you are using a custom RegisterController you should be able to call

$user->sendEmailVerificationNotification();

this in turn calls the

$this->notify(new Notifications\VerifyEmail);

This is because your user implements MustVerifyEmail in the Illuminate\Foundation\Auth\User.

EDIT----

You can also use the event new Registered which should be registered in the EventServiceProvider this will call the SendEmailVerificationNotification which in turn will call $event->user->sendEmailVerificationNotification();

event(new Registered($user = $this->create($request->all())));

Note: this works in laravel 5.7

Marcus
  • 1,850
  • 1
  • 12
  • 24
  • I indeed got a verification mail now! So thank you! But the mail doesn't work. It redirects to the login paga and doesnt verify the account. Any suggestions? – Kevin Walter Dec 12 '18 at 16:11
  • Your Auth::routes(['verify' => true]); should it not be in web.php because thats where you will have a session. If you do it in api.php you will not be logged in therefore you would not be able to submit anything. – Marcus Dec 13 '18 at 08:06