0

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

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

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

1 回答 1

0

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

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=用户名
MAIL_PASSWORD=密码
MAIL_FROM_ADDRESS=your_email@address.com
MAIL_FROM_NAME=YOURNAME
MAIL_ENCRYPTION=null

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>
于 2018-07-09T10:09:38.617 回答