首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel 5默认注册后如何发送邮件?

在Laravel 5默认注册后如何发送邮件?
EN

Stack Overflow用户
提问于 2015-03-20 18:03:23
回答 2查看 33.3K关注 0票数 21

我是laravel的新手,和Laravel 5一起工作。对于用户注册和登录,我想使用Laravel的默认系统。但是,需要通过以下两个特性来扩展它:

  1. 注册后,用户将收到一封电子邮件。
  2. 保存用户注册后,我需要在另一个角色表中输入一个条目(我已经使用Entrust包进行角色管理)

怎么做这些事?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-20 19:46:03

您可以修改位于app/services中的Laravel 5默认注册程序。

代码语言:javascript
复制
<?php namespace App\Services;

    use App\User;
    use Validator;
    use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
    use Mail;

    class Registrar implements RegistrarContract {

        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        public function validator(array $data)
        {
            return Validator::make($data, [
                'name' => 'required|max:255',
                'email' => 'required|email|max:255|unique:users',
                'password' => 'required|confirmed|min:6'
            ]);
        }

        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return User
         */
        public function create(array $data)
        {
            $user = User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => \Hash::make($data['password']),
                //generates a random string that is 20 characters long
                'verification_code' => str_random(20)
            ]);

//do your role stuffs here

            //send verification mail to user
            //---------------------------------------------------------
            $data['verification_code']  = $user->verification_code;

            Mail::send('emails.welcome', $data, function($message) use ($data)
            {
                $message->from('no-reply@site.com', "Site name");
                $message->subject("Welcome to site name");
                $message->to($data['email']);
            });


            return $user;
        }

    }

内部resources/emails/welcome.blade.php

代码语言:javascript
复制
Hey {{$name}}, Welcome to our website. <br>
Please click <a href="{!! url('/verify', ['code'=>$verification_code]) !!}"> Here</a> to confirm email

注意:您需要创建用于验证的路由/控制器。

票数 34
EN

Stack Overflow用户

发布于 2017-04-30 20:03:46

Laravel有一个名为Illuminate\Foundation\Auth\RegistersUsers特性中的注册的空方法来简化此操作,只需按如下方式重写它:

首先添加一个新的通知:

代码语言:javascript
复制
<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserRegisteredNotification extends Notification {

    public function __construct($user) {
        $this->user = $user;
    }

    public function via($notifiable) {
        return ['mail'];
    }

    public function toMail($notifiable) {
        return (new MailMessage)
            ->success()
            ->subject('Welcome')
            ->line('Dear ' . $this->user->name . ', we are happy to see you here.')
            ->action('Go to site', url('/'))
            ->line('Please tell your friends about us.');
    }

}

将此使用行添加到您的RegisterController.php中:

代码语言:javascript
复制
use Illuminate\Http\Request;
use App\Notifications\UserRegisteredNotification;

并添加此方法:

代码语言:javascript
复制
protected function registered(Request $request, $user) {
    $user->notify(new UserRegisteredNotification($user));
}

你完了。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29173028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档