Type error: Too few arguments to function Illuminate\Mail\Mailer::__construct(), 0 passed - Laravel 5.5

Viewed 20

I keep getting while trying to use the mailer

FatalThrowableError in Mailer.php line 93: Type error: Too few arguments to function Illuminate\Mail\Mailer::__construct(), 0 passed in /var/www/app/app/Services/SendOtpMail.php on line 42 and at least 2 expected

in Mailer.php line 93
at Mailer->__construct() in SendOtpMail.php line 42
at SendOtpMail->send('test@company.com', array('from' => 'no-reply@company.com', 'from_name' => 'Some Company', 'subject' => 'Login Verification', 'data' => array('token' => '3486', 'user' => object(User)), 'view' => 'emails.password')) in GetOtpForLoginService.php line 59
at GetOtpForLoginService->sendEmail('3486', object(User))

Send mail function

    public function sendEmail($otp, $user)
    {
        $user = User::where('email', $user->email)->firstOrFail();
        (new SendOtpMail())->send($user->email, [
                        'from' => env('MAIL_DEAFULT_SENDER'),
                        'from_name' => env('MAIL_DEAFULT_SENDER_ALIAS'),
                        'subject' => 'Login Verification',
                        'data' => [
                            'token' => $otp,
                            'user' => $user
                        ],
                        'view' => 'emails.password'
        ]);

        return true;
    }

SendOtpMail.php

<?php

namespace App\Services;

use Illuminate\Mail\Mailer;

class SendOtpMail
{
    public function send($to, array $options = array())

    {

        
        $callback = function($message) use ($options, $to) {
            $message->from($options['from'], isset($options['from_name']) ? $options['from_name'] : null);
            $message->to($to, isset($options['to_name']) ? $options['to_name'] : null);
            if(isset($options['subject'])) $message->subject($options['subject']);
            if(isset($options['priority'])) $message->priority($options['priority']);
            if(isset($options['priority'])) $message->priority($options['priority']);
            if(isset($options['files'])) {
                if (is_array($options['files'])) {
                    foreach ($options['files'] as $file) {
                       $message->attach($options[$file]);
                    }
                } else {
                    $message->attach($options['files']);
                }
            }
            if(isset($options['cc'])) $message->subject($options['cc'], isset($options['cc_name']) ? $options['cc_name'] : null);
            if(isset($options['bcc'])) $message->subject($options['bcc'], isset($options['bcc_name']) ? $options['bcc_name'] : null);

        };
        if(isset($options['view'])) {
            $data = isset($options['data']) ? $options['data'] : array();
            (new Mailer())->send($options['view'], $data, $callback);
        } else {
            (new Mailer())->raw($options['message'], $callback);
        }
    }
}
1 Answers

You are seeing that error because you are instantiating an Illuminate\Mail\Mailer object without specifying its required parameters in the constructor:

// from Laravel source code
public function __construct(string $name, Factory $views, TransportInterface $transport, Dispatcher $events = null)
{
    $this->name = $name;
    $this->views = $views;
    $this->events = $events;
    $this->transport = $transport;
}

I suggest you don't send emails this ways. Pls check the docs and follow the instructions.

Related