问题
Based on this documentation you can easily create a user email verification when someone signing up by them self, but how to send an email verification when admins created the account for their users?
I already tried with this approach
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\VerifiesEmails;
class TeacherController extends Controller
{
use VerifiesEmails;
... // Other basic functions
public function store(Request $request)
{
$rules = [
'first_name' => ['string', 'required', 'max:255'],
'last_name' => ['string', 'nullable', 'max:255'],
'email' => ['string', 'required', 'email', 'max:255', 'unique:users'],
'password' => ['string', 'required', 'min:6', 'confirmed'],
];
$request->validate($rules);
$teacher = \App\User::create([
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'role' => 'teacher',
]);
$teacher->sendEmailVerificationNotification();
return redirect()->route('teachers');
}
... // Other basic functions
}
but it's not working and no errors at all, but if I use $request->user()->sendEmailVerificationNotification();
it's working but send email verification for the admin instead. I already googled it but doesn't find the answer I want.
So how to solve this problem? can I achieve it with the default features from laravel or should I created by myself?
EDIT:: Here is the email that coming when i'm using $request->user()->sendEmailVerificationNotification();
, it's sending to admin@admin.com instead teacher@teacher.com
EDIT 2::
I already find the problem, it's because I'm using the VerifiesEmail
which is the core of the problems I had. Thanks to @nakov for his helps! :D
回答1:
In your EventServiceProvider
class have you registered the listener for the registered event?
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
and don't forget to import on top:
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
来源:https://stackoverflow.com/questions/53705321/laravel-5-7-send-an-email-verification-on-newly-created-user