php Laravel 5 默认注册后如何发送邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29173028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send mail after Laravel 5 default registration?
提问by Sovon
I'm noob in Laravel and working with Laravel 5. For user registration and and login, I want to use default system of laravel. But, need to extend it with two following features:
我是 Laravel 的菜鸟,正在使用 Laravel 5。对于用户注册和登录,我想使用 Laravel 的默认系统。但是,需要使用以下两个功能对其进行扩展:
- User will get an email just after registration.
- Upon saving of user registration, I need to make an entry in another role table (I've used Entrust package for role management)
- 用户将在注册后立即收到一封电子邮件。
- 保存用户注册后,我需要在另一个角色表中创建一个条目(我使用 Entrust 包进行角色管理)
How to do these things?
这些事情怎么办?
回答by Emeka Mbah
You can modify Laravel 5 default registrar located in app/services
您可以修改位于 app/services 中的 Laravel 5 默认注册器
<?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('[email protected]', "Site name");
$message->subject("Welcome to site name");
$message->to($data['email']);
});
return $user;
}
}
Inside resources/emails/welcome.blade.php
里面 resources/emails/welcome.blade.php
Hey {{$name}}, Welcome to our website. <br>
Please click <a href="{!! url('/verify', ['code'=>$verification_code]) !!}"> Here</a> to confirm email
NB: You need to create route/controller for verify
注意:您需要创建路由/控制器进行验证
回答by Sinan Eldem
Laravel has an empty method named registeredin Illuminate\Foundation\Auth\RegistersUserstrait to simplify this operation, just override it as following:
Laravel在Illuminate\Foundation\Auth\RegistersUserstrait 中有一个名为注册的空方法来简化此操作,只需将其覆盖如下:
First add a new Notification:
首先添加一个新的通知:
<?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.');
}
}
Add this useline to your RegisterController.php:
将此use行添加到您的 RegisterController.php:
use App\Notifications\UserRegisteredNotification;
and add this method:
并添加此方法:
protected function registered(Request $request, $user) {
$user->notify(new UserRegisteredNotification($user));
}
You are done.
你完成了。