Laravel 5.7向新创建的用户发送电子邮件验证 [英] Laravel 5.7 Send an email verification on newly created user

查看:41
本文介绍了Laravel 5.7向新创建的用户发送电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此

编辑2 ::我已经发现问题了,这是因为我正在使用 VerifyEmail ,这是我遇到的问题的核心.感谢@nakov的帮助!:D

解决方案

在您的 EventServiceProvider 类中,您是否已为已注册事件注册了侦听器?

 /***应用程序的事件侦听器映射.** @var数组*/受保护的$ listen = [已注册:: class =>[SendEmailVerificationNotification :: class,],]; 

不要忘了在顶部导入:

 使用Illuminate \ Auth \ Events \ Registered;使用Illuminate \ Auth \ Listeners \ SendEmailVerificationNotification; 

Based on this documentation you can easily create a user email verification when someone signing up by them self, but how to send an email verification when admins created the account for their users?

I already tried with this approach

<?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

}

but it's not working and no errors at all, but if I use $request->user()->sendEmailVerificationNotification(); it's working but send email verification for the admin instead. I already googled it but doesn't find the answer I want.

So how to solve this problem? can I achieve it with the default features from laravel or should I created by myself?

EDIT:: Here is the email that coming when i'm using $request->user()->sendEmailVerificationNotification();, it's sending to admin@admin.com instead teacher@teacher.com

EDIT 2:: I already find the problem, it's because I'm using the VerifiesEmail which is the core of the problems I had. Thanks to @nakov for his helps! :D

解决方案

In your EventServiceProvider class have you registered the listener for the registered event?

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

and don't forget to import on top:

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

这篇关于Laravel 5.7向新创建的用户发送电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆