【问题标题】:Laravel 4: how to make confirmation email?Laravel 4:如何制作确认邮件?
【发布时间】:2013-08-31 04:45:32
【问题描述】:

到目前为止,我已经制作了一个带有登录/注册功能的应用程序,它运行良好。 注册后会发送一封欢迎电子邮件。

但我想做的是在该邮件中发送一个链接,只有在点击它之后才能登录。

比如论坛常用的注册邮箱等。

有人可以帮帮我吗?

这是 postRegister 方法:

public function postRegister()
{
    $input = Input::all();

    $rules = array(
        'username' => 'required',
        'password' => 'required');

    $validation = Validator::make($input, $rules);

    if ($validation->passes()) {

        $password = $input['password'];
        $password = Hash::make($password);

        $user = new User;
        $user->username = $input['username'];
        $user->email = $input['email'];
        $user->password = $password;

            $mailer = new Mailers\UserMailer($user);

                 // var_dump($mailer);

                    $mailer->welcomeMail()->deliver();

                    $user->save();

        return Redirect::to('afterRegister');
    }

    return Redirect::back()->withInput()->withErrors($validation)->with('message', 'Validation Errors!');
} 

谢谢

【问题讨论】:

    标签: email laravel laravel-4 confirmation-email


    【解决方案1】:

    这里有一些线索(不会为你写代码)。

    • 在您的用户表中添加两个字段:confirmationconfirmed
    • 在 Laravel 中创建一条类似 registration/verify/{confirmation} 的路由,您可以在其中尝试使用给定的确认码在数据库中查找用户(如果找到,请将用户的 confirmed 字段设置为 1)。
    • 在用户注册后,生成一个唯一的确认码(您可以使用str_random() 帮助函数)。
    • 相应地设置新用户的数据库条目(confirmation = 随机码,confirmed = 0)
    • 在发送给新用户的电子邮件中包含一个验证链接(根据您的验证路线构建)和生成的确认码。

    现在可以像这样进行身份验证尝试:

    $user = array(
            'username' => Input::get('username'),
            'password' => Input::get('password'),
            'confirmed' => 1
    );
    
    if (Auth::attempt($user)) {
        // success!
        return Redirect::route('restricted/area');
    }
    

    【讨论】:

    • 如果只确认为假,是否可以设置自定义消息。因此您会收到一条错误消息,例如该帐户尚未注册。
    • @Sven B 你可以做 Redirect::route('restricted/area')->with('message', 'Account is not registered');并在您的视图中使用 {{ $message }}。
    • 我不喜欢使用“已确认”栏。当用户被激活时,我只需删除确认码。这样,所有没有确认令牌的用户都会被激活。
    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多