简体   繁体   中英

Laravel send activation link to a specific email

I want the activation link after registration to send to one email, this because I don't want everyone to create an admin account, so anyone create an admin account the owner of the app will activate his account by clicking on the activation link on his email (the owner email).

protected function postAdminRegistration(Request $request)
{
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);

    try {
        $validatedData['password'] = bcrypt(array_get($validatedData, 'password'));
        $validatedData['activation_code'] = str_random(30).time();
        $user = app(User::class)->create($validatedData);
    } catch (\Exception $exception) {
        logger()->error($exception);
        return redirect()->back()->with('message', 'Unable to create new user.');
    }
    $user->notify(new UserRegisteredSuccessfully($user));

    return redirect()->route("user.loginform")->withSuccess('Successfully created a new account.
        Please check your email and activate your account.');
}

UserRegisteredSuccessfully

public function toMail($notifiable)
{
    $user = $this->user;

    return (new MailMessage)
        ->from('****@gmail.com')
        ->subject('Successfully created new account')
        ->greeting(sprintf('Hello %s', $user->fname))
        ->line('You have successfully registered to our system. Please activate your account.')
        ->action('Click Here',
            route('user.activate', $user->activation_code))->line('Thank you for using our application!');
}

Model

class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name','email','password'
];

protected static $logFillable = true;
protected $hidden = [
    'password', 'remember_token',
];

public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token.'/'.$this->email));
}
}

add to method in your code

return (new MailMessage)
    ->from('****@gmail.com')
    ->to("xyz@email.com")

     ...

for more details https://laravel.com/docs/6.x/notifications

Can't add a comment, so i post the possible answer this way; i assume you can add the recipient by ->to('someone@somewhere.not')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM