Home > Mobile >  Too few arguments to function, 0 passed in and exactly 1 expected when using Mail
Too few arguments to function, 0 passed in and exactly 1 expected when using Mail

Time:09-11

I'm trying to send a simple email using the Mailable class. I've followed this tutorial online to the tee and I'm still getting this error. I have no idea what the problem could be because everything seems fine and I've followed the tutorial step for step. What am I doing wrong?

TestMail.php

<?php

namespace App\Mail;

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

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Test Mail from Duke')->view('emails.TestMail');
    }
}

MailController

<?php

namespace App\Http\Controllers;

use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    //
    public function sendEmail()
    {
        $details = [
            'title' => 'The Title',
            'body' => 'The body should be fine',
        ];
        Mail::to("[email protected]")->send(new TestMail());
        return "Email Sent";
    }
}

The route

<?php

use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/send', [MailController::class, 'sendEmail']);

CodePudding user response:

The TestMail object expects a $details parameter passed in its constructor, yet none was supplied.

(MailController)

Instead of:❌

 Mail::to("[email protected]")->send(new TestMail());

Use this:✅

 Mail::to("[email protected]")->send(new TestMail($details));

Addendum

I suppose you intended to pass the $details to the View. Hence:

(TestMail.php)

// ...

return $this
    ->subject('Test Mail from Duke')
    ->view('emails.TestMail')
    ->with("details", $this->details);

// ...
  • Related