如何在电子邮件中发送用户的凭据

How to send user's credentials in email

提问人:cdBreak 提问时间:11/2/2022 更新时间:11/2/2022 访问量:188

问:

在我的 EventServiceProvider 中,我有一个事件和侦听器,如下所示

 protected $listen = [
        'App\Events\UserCreated' => [
            'App\Listeners\SendCredentials'
        ],
    ];

当管理员创建用户时,他们的密码会自动生成,因此为了让他们访问他们的帐户,我必须通过电子邮件向他们发送他们的凭据

class UserCreated
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $user;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

}
class SendCredentials
{
    /**
     * Handle the event.
     *
     * @param  \App\Events\UserCreated  $event
     * @return void
     */
    public function handle(UserCreated $event)
    {
        $user = $event->user;
        Mail::to($user->email)->send(new SendUserMail($user));
    }
}

这就是我调度 UserCreated 事件的方式

public function createUser(){
        $this->validate();

        $randomPass = Str::random(8);
        $userCreds = ['email' => $this->email, 'password' => $randomPass];

        try{
            $test = User::firstOrCreate([
                'name' => $this->name,
                'email' => $this->email,
                'password' => Hash::make($randomPass),
                'email_verified_at' => Carbon::now(),
                'user_type' => (int)$this->selectedUser,
                'assigned_to' => (int)$this->selectedAssigned,
                'doctor_type' => $this->selectTypeDoc,
            ]);

            // Mail::to($this->email)->send(new SendUserMail($userCreds));
            UserCreated::dispatch($test);

            $this->reset();
            $this->emitUp('refreshParent');
            $this->dispatchBrowserEvent('swal-insert');

        }catch(\Exception $e){
            $this->dispatchBrowserEvent('createUser-error');
        }

        $this->closeModal();

    }

如何在电子邮件数据中发送未加密的密码

Laravel 事件 侦听器

评论


答:

1赞 Michal D 11/2/2022 #1

创建用户并对密码进行哈希处理时,无法以可读的方式检索密码。

在你的情况下,我会做的是改变如下:UserCreated

class UserCreated
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    public $password;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user, $password)
    {
        $this->user = $user;
        $this->password = $password;
    }

}

我还会考虑使用 getter 和 setter,而不是将它们公开。UserCreated

和你的方法createUser

public function createUser(){
        $this->validate();

        $randomPass = Str::random(8);
        $userCreds = ['email' => $this->email, 'password' => $randomPass];

        try{
            $test = User::firstOrCreate([
                'name' => $this->name,
                'email' => $this->email,
                'password' => Hash::make($randomPass),
                'email_verified_at' => Carbon::now(),
                'user_type' => (int)$this->selectedUser,
                'assigned_to' => (int)$this->selectedAssigned,
                'doctor_type' => $this->selectTypeDoc,
            ]);

            // Mail::to($this->email)->send(new SendUserMail($userCreds));
            UserCreated::dispatch($test, $randomPass);

            $this->reset();
            $this->emitUp('refreshParent');
            $this->dispatchBrowserEvent('swal-insert');

        }catch(\Exception $e){
            $this->dispatchBrowserEvent('createUser-error');
        }

        $this->closeModal();

    }

SendCredentials

class SendCredentials
{
    /**
     * Handle the event.
     *
     * @param  \App\Events\UserCreated  $event
     * @return void
     */
    public function handle(UserCreated $event)
    {
        Mail::to($user->email)->send(new SendUserMail($event->user, $event->password));
    }
}