【问题标题】:Laravel 5.7 Send an email verification on newly created userLaravel 5.7 向新创建的用户发送电子邮件验证
【发布时间】:2019-05-11 07:59:05
【问题描述】:

基于此documentation,当有人自行注册时,您可以轻松创建用户电子邮件验证,但是当管理员为其用户创建帐户时如何发送电子邮件验证?

我已经尝试过这种方法

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\VerifiesEmails;

class TeacherController extends Controller
{
    use VerifiesEmails;

    ... // Other basic functions

    public function store(Request $request)
    {
        $rules = [
            'first_name' => ['string', 'required', 'max:255'],
            'last_name' => ['string', 'nullable', 'max:255'],
            'email' => ['string', 'required', 'email', 'max:255', 'unique:users'],
            'password' => ['string', 'required', 'min:6', 'confirmed'],
        ];

        $request->validate($rules);

        $teacher = \App\User::create([
            'first_name' => $request->input('first_name'),
            'last_name' => $request->input('last_name'),
            'email' => $request->input('email'),
            'password' => Hash::make($request->input('password')),
            'role' => 'teacher',
        ]);

        $teacher->sendEmailVerificationNotification();

        return redirect()->route('teachers');
    }

    ... // Other basic functions

}

但它不工作并且根本没有错误,但如果我使用 $request-&gt;user()-&gt;sendEmailVerificationNotification(); 它工作但发送电子邮件验证给管理员。我已经用谷歌搜索过了,但没有找到我想要的答案。

那么如何解决这个问题呢?我可以使用 laravel 的默认功能实现它还是我应该自己创建?

编辑:: 这是我使用$request-&gt;user()-&gt;sendEmailVerificationNotification(); 时收到的电子邮件,它发送到 admin@admin.com 而不是 teacher@teacher.com

编辑 2:: 我已经找到了问题,因为我使用的是VerifiesEmail,这是我遇到的问题的核心。感谢@nakov 的帮助! :D

【问题讨论】:

  • 让我知道你在用什么本地服务器和其他
  • @Krishnakushwaha 是的,我使用的是本地服务器,也使用了望远镜

标签: php laravel email-verification


【解决方案1】:

在您的EventServiceProvider 课程中,您是否为已注册的事件注册了监听器?

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ],
];

别忘了在上面导入:

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;

【讨论】:

  • 是的,我已经这样做了,但是我注意到SendEmailVerificationNotification() 处理了一个注册用户,但是刚刚创建的用户已经被认为是注册的。这里是 SendEmailVerificationNotification.php 内容public function handle(Registered $event) { if ($event-&gt;user instanceof MustVerifyEmail &amp;&amp; ! $event-&gt;user-&gt;hasVerifiedEmail()) { $event-&gt;user-&gt;sendEmailVerificationNotification(); } }
  • 对于已经注册的用户来说很好,一旦他们登录,如果你用经过验证的中间件包围了路由,它将向他们显示发送带有链接的电子邮件的页面。所以我提到的应该适用于新用户。
  • 是的,确实,当他们尝试登录时,需要先重新发送电子邮件验证,但我不认为这很漂亮。所以我希望管理员创建帐户后,将向新创建的帐户电子邮件地址发送电子邮件验证
  • 这就是你应该注册监听器的原因。但是如果管理员正在注册老师,他们怎么知道密码呢?即使他们收到一封电子邮件来验证他们的帐户,他们也需要能够登录到他们的帐户?您是否出于某种原因覆盖了mail.php 中的to 配置以向admin@admin.com 发送邮件?
  • 他们将获得电子邮件所附的密码,但现在我希望应用程序为每个创建的用户发送电子邮件验证(不是从注册开始)。不,我不会将电子邮件覆盖到admin@admin.com。它被发送到admin@admin.com 是因为现在登录的用户。如果我想使用默认的 laravel 电子邮件验证,我会遗漏什么吗?因为他们的文档没有告诉我们如何将它用于另一个控制器等。
猜你喜欢
  • 2019-03-05
  • 2023-03-28
  • 1970-01-01
  • 2019-10-18
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2019-07-05
相关资源
最近更新 更多