我已将laravel实例从5.6版升级到5.7版。现在,我尝试使用built-in email verification from laravel。
我的问题是,当我使用“重新发送”功能到达电子邮件时,成功注册后没有收到电子邮件。
出什么问题了?
答案 0 :(得分:6)
我遇到了完全相同的问题。那是Laravel的默认代码。
要在成功注册后发送电子邮件,您可以执行以下解决方法:
在App \ Http \ Controllers \ Auth \ RegisterController中
更改此内容:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
为此:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->sendEmailVerificationNotification();
return $user;
}
答案 1 :(得分:5)
我也遇到过同样的问题。当我检查源代码时,不必实现调用sendEmailVerificationNotfication()
方法,您只需将事件处理程序添加到EventServiceProvider.php
中,因为事件处理程序是先前创建的,因此Larael无法更新。它应该看起来像这样:
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
答案 2 :(得分:1)
以防其他人正在寻找解决同一问题的方法。
请阅读文档,其中确切说明了解决此问题需要做什么
https://laravel.com/docs/5.7/verification
简而言之,如果您已经在使用5.7(即users
表中有必填字段),则只需执行以下操作:
User
模型实现MustVerifyEmail
接口。['verify' => true]
添加到Auth::routes
方法Auth::routes(['verify' => true]);
您可以在上面的链接中找到有关电子邮件验证的所有信息。
答案 3 :(得分:1)
除了djug的答复外,如果从5.6版升级后遇到同样的问题,就像我一样,您会在这里找到逐步实施的指南:
https://laravel.com/docs/5.7/upgrade
电子邮件验证
部分下希望这对某人有所帮助,因为我为此花了很多时间。
答案 4 :(得分:1)
我知道这篇文章有点旧,但我在 Laravel 7 上遇到了类似的问题。我认为上面 Zane 的答案应该是公认的答案。只是为了详细说明使用以下步骤。这应该在使用 composer 和 php artisan 安装 auth scaffolding 之后完成。注意:我绝不是 Laravel 专家。如果我的代码有任何问题,请告诉我。我学得越多越好。
确保您的 App\User 模型实现 Illuminate\Contracts\Auth\MustVerifyEmail:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
在 routes\web.php 中更改:
Auth::routes();
为此:
Auth::routes(['verify'=>true]);
之后,您可以通过直接在路由上使用中间件来指定哪些路由需要经过验证的电子邮件地址:
Route::get('/profile','ProfileController@index')->middleware('verified');
或者你可以在控制器的构造函数中这样做:
public function __construct()
{
$this->middleware(['auth','verified']);
}
我正在使用以下寄存器控制器代码。请注意,注册函数包含对以下内容的调用:
event(new Registered($user));
这是发送初始注册电子邮件的关键。
请记住,此控制器主要用于 ajax 站点,因此注册函数返回 json 响应。
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use Auth;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone_number' => ['required', 'numeric', 'min:10'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'password_confirmation'=> ['required', 'string'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$user=User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'password' => Hash::make($data['password']),
]);
return $user;
}
/**
* Execute registration and login the user
*
* @param array $request
* @return response
*/
public function register(Request $request) {
$validation = $this->validator($request->all());
if ($validation->fails()) {
return response()->json($validation->errors(),422);
}
else{
$user = $this->create($request->all());
event(new Registered($user));
Auth::login($user);
if (Auth::user()){
return response()->json(['success'=>'Registration Successful']);
}
}
}
}
答案 5 :(得分:0)
如果您有一个自定义注册页面,则可以在创建用户后触发事件,如下所示:
event(new Registered($user));
答案 6 :(得分:-1)
请确保已设置“发件人”,因为大多数SMTP服务器都不允许从任何地址发送邮件。这些的环境配置是:
MAIL_FROM_ADDRESS=from@domain.com
MAIL_FROM_NAME=Something