简体   繁体   English

Laravel 将激活链接发送到特定电子邮件

[英]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更多详情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')我假设您可以通过->to('someone@somewhere.not')添加收件人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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