【问题标题】:User verification email cannot be sent in laravellaravel 无法发送用户验证邮件
【发布时间】:2020-07-29 13:51:27
【问题描述】:

在我的 laravel 基础应用程序中,我有一个管理面板,我的管理员可以在其中从后端添加新用户。

但我想在管理员创建帐户后向我的用户发送帐户激活电子邮件。

当用户从前端注册表单向系统注册时,我已经发送了帐户激活电子邮件。

但是当管理员创建帐户时,我无法向用户发送激活邮件..

以下是我的用户模型

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
    use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name','last_name', 'email', 'password','username','mobile','user_roles',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

和我的用户控制器(只包含store功能)如下

public function store(Request $request)
    {
        request()->validate([
            'name' => ['required', 'alpha','min:2', 'max:255'],
            'last_name' => ['required', 'alpha','min:2', 'max:255'],
            'email' => ['required','email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:12', 'confirmed','regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/'],
            'mobile'=>['required', 'regex:/^\+[0-9]?()[0-9](\s|\S)(\d[0-9]{9})$/','numeric','min:9'],
            'username'=>['required', 'string', 'min:4', 'max:10', 'unique:users'],   
            'roles'=>['required'],
            'user_roles'=>['required'],
        ]);

        //Customer::create($request->all());

        $input = $request->all();
        $input['password'] = Hash::make($input['password']);

        $user = User::create($input);
        $user->assignRole($request->input('roles'));

        return redirect()->route('customers.index')
                        ->with('success','Customer created successfully.');
    }

在我的 web.php 中有这个,

Auth::routes(['verify' => true]);

所以目前用户创建成功但验证邮件没有发送。

如何发送验证邮件?我需要在哪里更改?

谢谢。

【问题讨论】:

    标签: php laravel laravel-5 laravel-6 email-verification


    【解决方案1】:

    创建用户后,使用新用户实例触发Registered 事件。

    event(new Registered($user));
    

    【讨论】:

    • 成功了!拯救我的伙伴。
    • 非常感谢
    【解决方案2】:

    在返回之前添加这个

    $user->sendEmailVerificationNotification();
    

    这将向用户发送验证通知

    【讨论】:

      猜你喜欢
      • 2019-10-29
      • 1970-01-01
      • 2021-02-03
      • 2021-09-25
      • 2022-08-13
      • 2019-03-05
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多