【问题标题】:Laravel send activation link to a specific emailLaravel 将激活链接发送到特定电子邮件
【发布时间】:2020-04-20 19:08:43
【问题描述】:

我希望注册后的激活链接发送到一个电子邮件,这是因为我不希望每个人都创建一个管理员帐户,所以任何人创建一个管理员帐户应用程序的所有者将通过点击激活来激活他的帐户他的电子邮件(所有者电子邮件)上的链接。

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.');
}

用户注册成功

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!');
}

型号

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));
}
}

【问题讨论】:

  • 您的意思是将激活链接发送到特定的电子邮件?那么您必须添加此电子邮件
  • 我必须把这封电子邮件放在哪里?之后->来自('****@gmail.com')?
  • 我认为这是您获取电子邮件的唯一途径
  • 有什么更新吗?
  • @FaragHalain 您要将激活电子邮件发送给谁(发送至owner of the app 或`注册用户`)?

标签: php laravel


【解决方案1】:

添加到代码中的方法

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

     ...

更多详情https://laravel.com/docs/6.x/notifications

【讨论】:

  • 你能解释更多吗?必须在哪里添加这个?如果从那以后这将不起作用...调用未定义的方法 Illuminate\Notifications\Messages\MailMessage::to()
  • ,添加型号,请查看
  • 我需要一个解决方案来解决我的情况,你发送的我以前见过,不知道它是否适用于我的情况,所以如果可能的话就在这里写一个代码thnkx
  • 使用 mailables 发送邮件laravel.com/docs/5.8/mail#writing-mailables
【解决方案2】:

无法添加评论,所以我以这种方式发布可能的答案;我假设您可以通过以下方式添加收件人 ->to('someone@somewhere.not')

【讨论】:

  • 你能解释更多吗?必须在哪里添加这个?如果从那以后这将不起作用...调用未定义的方法 Illuminate\Notifications\Messages\MailMessage::to()
  • 很遗憾没有找到答案
猜你喜欢
  • 2016-07-05
  • 2018-07-24
  • 1970-01-01
  • 2017-09-24
  • 2014-10-20
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 2013-02-17
相关资源
最近更新 更多