Laravel 5.7 - 不发送验证邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52569442/
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
Laravel 5.7 - Verification email is not sent
提问by Markus
I've upgraded my laravel instance from version 5.6 to version 5.7. Now I try to use the built-in email verification from laravel.
我已经将我的 laravel 实例从 5.6 版升级到了 5.7 版。现在我尝试使用laravel的内置电子邮件验证。
My problem is that I don't get an email after successful registration when I use the "resend" function the email arrives.
我的问题是,当我使用电子邮件到达的“重新发送”功能时,我在成功注册后没有收到电子邮件。
What is the problem?
问题是什么?
回答by Jorge Augusto Morêra de Moura
I had this exactly same problem. That is default code from Laravel.
我遇到了完全相同的问题。这是 Laravel 的默认代码。
In order to send the email after successful registration you can do this workaround:
为了在成功注册后发送电子邮件,您可以执行以下解决方法:
at App\Http\Controllers\Auth\RegisterController
在 App\Http\Controllers\Auth\RegisterController
change this:
改变这个:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
to this:
对此:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->sendEmailVerificationNotification();
return $user;
}
回答by laze
I also have had the same issue. As I checked the source code, it isn't necessary to implement to call the sendEmailVerificationNotfication()
method, you just should add the event handler to your EventServiceProvider.php
, as because of your event handler was previously created, so Larael can't update that. It should look like this:
我也有同样的问题。当我检查源代码时,没有必要实现调用该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,
],
];
回答by djug
in case somebody else is looking for a solution for the same problem.
以防其他人正在为同一问题寻找解决方案。
please read the documentation, it explains exactly what needs to be done to solve this issue
请阅读文档,它准确地解释了解决此问题所需的操作
https://laravel.com/docs/5.7/verification
https://laravel.com/docs/5.7/verification
in a nutshell, and if you are already using 5.7 (i.e you have the necessary fields in your users
table) all that you need to do is the following:
简而言之,如果您已经在使用 5.7(即您的users
表中有必要的字段),您需要做的就是以下内容:
- make your
User
model implement theMustVerifyEmail
interface. - add
['verify' => true]
to theAuth::routes
methodAuth::routes(['verify' => true]);
- 让你的
User
模型实现MustVerifyEmail
接口。 - 添加
['verify' => true]
到Auth::routes
方法中Auth::routes(['verify' => true]);
you can find everything you need about email verification in the link above.
您可以在上面的链接中找到有关电子邮件验证的所有信息。
回答by Adam Brzeziński
In addition to djug's reply, if you experience the same problem after upgrading from version 5.6, just as I did, you'll find step-by-step guide what to implement here:
除了 djug 的回复,如果您在从 5.6 版升级后遇到同样的问题,就像我所做的一样,您将在此处找到逐步指南:
https://laravel.com/docs/5.7/upgrade
https://laravel.com/docs/5.7/upgrade
under the section Email Verification
在电子邮件验证部分下
Hope this helps someone as I was struggling quite some time with this.
希望这对某人有所帮助,因为我为此苦苦挣扎了一段时间。
回答by Zane
If you have a custom registration page, you could just fire the event after you have created the user like so:
如果您有自定义注册页面,则可以在创建用户后触发该事件,如下所示:
event(new Registered($user));
event(new Registered($user));
回答by phoenix
Make sure you have your From Email setup as most SMTP servers won't allow sending from any address. The env config for those are:
确保你有你的发件人电子邮件设置,因为大多数 SMTP 服务器不允许从任何地址发送。这些的 env 配置是:
[email protected]
MAIL_FROM_NAME=Something