尝试在Laravel中发送电子邮件时出错

时间:2017-12-03 09:00:27

标签: laravel laravel-5 laravel-mail laravel-events

当有人注册时,我试图发送一些电子邮件来发送电子邮件执行代码。所以我把它变成了一个名为UserRegistered的事件。这是我调用事件的RegisterController

公共功能商店(请求$请求){

    $this->validate( $request,
        [
            'name' => 'required|min:3',
            'username' => 'required|unique:users|min:3',
            'email' => 'required|email|unique:users',
            'password' => 'required|confirmed|min:12'
        ]
    );

    $user = User::create([
        'name' => request('name'),
        'email' => request('email'),
        'username' => request('username'),
        'password' => bcrypt(request('password')),
        'token' => random(30),
    ]);

    event(new UserRegistered($user));

    return back()->with('success','Please check your email to active your account.');
}

在UserRegistered i上传递用户数据,如下所示:

 class UserRegistered
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $user;



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

        /**
         * Get the channels the event should broadcast on.
         *
         * @return \Illuminate\Broadcasting\Channel|array
         */
        public function broadcastOn()
        {
            return new PrivateChannel('channel-name');
        }

这是我的听众:

class SendActivationCode
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserRegistered  $event
     * @return void
     */
    public function handle(UserRegistered $event)
    {
        Mail::to($event)->send(new SendActivationMail($event));
    }
}

这里是SendActivationMail类:

class SendActivationMail extends Mailable
{
    use Queueable, SerializesModels;
    protected $user;
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct(User $user)
        {
            $this->user = $user;
        }

        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            return $this->markdown('mails.user.activation')->with([
                'name' => $this->user->name,
                'token' => $this->user->token,
            ]);
         }
}

但我得到了这个错误:

"Type error: Argument 1 passed to App\Mail\SendActivationMail::__construct() must be an instance of App\User, instance of App\Events\UserRegistered given, called in E:\laragon\www\blog\app\Listeners\SendActivationCode.php on line 33 ◀"

我的activation.blade.php:

@component('mail::message')
# Activation Code for {{ config('app.name') }}

Hi {{ $user->name }} You are recieved this because you are registered at our site. To active your account please click the link bellow.

@component('mail::button', ['url' => route('verify').$user->token])
Active Account
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

那么什么可以让我得到这个错误以及如何解决它?

2 个答案:

答案 0 :(得分:2)

您的SendActivatiionMail课程似乎没有延伸Illuminate\Mail\Mailable。请在SendActivatiionMail.php中尝试此操作:

    namespace App\Mail;

    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;

    class SendActivatiionMail extends Mailable
    {
        use Queueable, SerializesModels;
    ...

然后在你的事件监听器中:

use App\Mail\SendActivationMail;

public function handle(UserRegistered $event)
{
    Mail::to($event->user->email)->send(new SendActivationMail($event->user));
}

答案 1 :(得分:0)

您的SendActivatiionCode必须扩展可邮寄的类别。

<?php

use Illuminate\Mail\Mailable;

class SendActivatiionCode extends Mailable {
    ...
}