2

This is my first time using events in Laravel/Lumen.

I am actually using Lumen and I am trying to dispatch an instance of Mailable when a new user signs up in order to send an email in the background.

I believe I have set it up right, but I keep getting this error...

Type error: Argument 1 passed to Illuminate\Mail\Mailable::queue() must implement interface Illuminate\Contracts\Queue\Factory, instance of Illuminate\Queue\DatabaseQueue given

I can't actually see within the error message itself where the issue is coming from e.g. there is no line numbers.

However, this is my code...

AuthenticationContoller.php

$this->dispatch(new NewUser($user));

NewUser.php

<?php

namespace App\Mail;

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

class NewUser extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    protected $user;

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

    /**
      * Build the message.
      *
      * @return $this
      */
      public function build()
      {
         return $this->view('test')->to('[email protected]', 'Test')
        ->from('[email protected]', 'test')->replyTo('[email protected]', 'test')
        ->subject('Welcome to the blog!');
      }
}

1 Answer 1

0

I ran into the same issue. It seems like Lumen and Illuminate/Mailer don't work together all that well.

However I found quite an easy fix in a Github thread.

Basically you just have to create a new service provider in your app/Providers directory.

MailServiceprovider.php

<?php

namespace App\Providers;

use Illuminate\Mail\Mailer;
use Illuminate\Mail\MailServiceProvider as BaseProvider;

class MailServiceProvider extends BaseProvider
{
    /**
     * Register the Illuminate mailer instance.
     *
     * @return void
     */
    protected function registerIlluminateMailer()
    {
        $this->app->singleton('mailer', function ($app) {
            $config = $app->make('config')->get('mail');

            // Once we have create the mailer instance, we will set a container instance
            // on the mailer. This allows us to resolve mailer classes via containers
            // for maximum testability on said classes instead of passing Closures.
            $mailer = new Mailer(
                $app['view'], $app['swift.mailer'], $app['events']
            );

            // The trick
            $mailer->setQueue($app['queue']);

            // Next we will set all of the global addresses on this mailer, which allows
            // for easy unification of all "from" addresses as well as easy debugging
            // of sent messages since they get be sent into a single email address.
            foreach (['from', 'reply_to', 'to'] as $type) {
                $this->setGlobalAddress($mailer, $config, $type);
            }

            return $mailer;
        });

        $this->app->configure('mail');

        $this->app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);
    }
}

And then you just have to register this service provider in your bootstrap/app.php instead of the default one by adding the following line:

$app->register(\App\Providers\MailServiceProvider::class);

Sign up to request clarification or add additional context in comments.

2 Comments

$app->make('queue'); will work, add this to your app.php
HI i am trying to do this in lumen 8, but getting another problem showing Target class [mail.manager] does not exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.