如何使用Laravel 5.4使用Cartalyst哨兵发送激活电子邮件/代码

时间:2018-07-09 07:40:08

标签: php laravel cartalyst-sentinel

我想在注册过程中向他们的电子邮件发送激活电子邮件/代码,但是我找不到合适的答案来帮助我完成工作。

这是我的控制器方法,其中我使用哨兵将用户数据保存到数据库中。

public function postRegister(Request $request)
{
    $user = Sentinel::registerAndActivate($request->all());
    return redirect('/');
}

我想在用户注册时向他们的电子邮件发送激活电子邮件/代码。

您的任何帮助将不胜感激!

                     public function postRegister(Request $request)
{
    $user = Sentinel::register($request->all());
    $activation = Activation::create($user);

    $this->sendEmail($user, $activation->code);
    return redirect('/');
}

private function sendEmail($user,$code)
{
    Mail::send('emails.activation',[

            'user' => $user,
            'code' => $code
    ], function($message) use ($user){
        $message->to($user->email);
        $message->subject("Hello $user->first_name,
        activate your account.");
    });
}

1 个答案:

答案 0 :(得分:0)

使用此代码
此外,请在控制器中使用MAIL名称空间,不要忘记创建email.verify刀片在应用程序路径resources \ views \ email中,并在.env文件中创建smtp详细信息。

MAIL_DRIVER = smtp
MAIL_HOST = smtp.mailtrap.io
MAIL_PORT = 2525
MAIL_USERNAME =用户名
MAIL_PASSWORD =密码
MAIL_FROM_ADDRESS =您的电子邮件@ address.com
MAIL_FROM_NAME = YOURNAME
MAIL_ENCRYPTION =空

public function postRegister(Request $request)
    {
        $verification_code = 111111;   // create random number & save it to db ;
        $user = Sentinel::registerAndActivate($request->all());
        return redirect('/');

        $subject = "Please verify your email address.";
        Mail::send('email.verify', ['name' => $user->name, 'verification_code' => $verification_code],
            function ($mail) use ($user, $subject) {
                $mail->from(getenv('FROM_EMAIL_ADDRESS'), "YOUR APPLICATION NAME");
                $mail->to($user->email, $user->name);
                $mail->subject($subject);
            });
    }

这是您的email.verify刀片服务器:

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
</head>
<body>

<div>
    Hi {{ $name }},
    <br>
    Thank you for creating an account with us.
    <br>
    Your verification code : <b>{{$verification_code}}</b>
    <br/>
</div>

</body>
</html>