Home > Software design >  Illuminate\Mail\PendingMail::send(): Argument #1 ($mailable) must be > of type Illuminate\Cont
Illuminate\Mail\PendingMail::send(): Argument #1 ($mailable) must be > of type Illuminate\Cont

Time:09-12

This is the error I get when I am trying to send an email with Laravel:

Illuminate\Mail\PendingMail::send(): Argument #1 ($mailable) must be of type Illuminate\Contracts\Mail\Mailable, App\Http\Livewire\Auth\ResetPassword given, called in /Users/andreiraileanu/Development/Protein/app/Http/Livewire/Auth/ResetPassword.php

So I have a livewire component where I reset the user password:

<?php

namespace App\Http\Livewire\Auth;
 
use Livewire\Component;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Mail;
use App\Models\User;

class ResetPassword extends Component
{
    public $email;
    public $phone;
    protected $user;

    protected $rules = [
        'email' => 'required|email',
        'phone' => 'required|digits:10',
    ];

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

        $this->user = User::where('email', $this->email)
                ->where('phone', $this->phone)
                ->first();
        if ($this->user) {
            
            \Debugbar::info('Identified User ID: '.$this->user->id);

            $this->user->password = Str::random(8);
            \Debugbar::info('New password is: '.$this->user->password);
            
            $this->user->password = Hash::make($this->user->password);
            $this->user->save();
            \Debugbar::info('Password saved!');
            
            Mail::to($this->user->email)->send(new ResetPassword($this->user));
        } else {
            $this->dispatchBrowserEvent('notify', ['content' => 'Adresa de email nu există sau numărul de telefon este incorect', 'type' => 'error']);
            \Debugbar::error('Wrong email and/or phone number.');
        }
    }

    public function render()
    {
        return view('livewire.auth.reset-password');
    }
}

In this component I have a line where I try to email the new password like this Mail::to($this->user->email)->send(new ResetPassword($this->user)); but I get the error above.

This is what my Mailable looks like:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;

class ResetPassword extends Mailable
{
    use Queueable, SerializesModels;

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->subject('Order Shipped')
            ->view('emails.auth.reset-password');
    }
}

What am I doing wrong? Never encountered this error before.

CodePudding user response:

you are calling the class itself in your App\Http\Livewire\Auth\ResetPassword and not your ResetPassword Mailable class from App\Mail

in your App\Http\Livewire\Auth\ResetPassword you need to call the ResetPassword which is from your App\Mail like so;

namespace App\Http\Livewire\Auth;
 
use Livewire\Component;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Mail;
use App\Models\User;
use App\Mail\ResetPassword as ResetPasswordMail;

then use use it like so;

Mail::to($this->user)->send(new ResetPasswordMail($this->user));
  • Related