Framework: Mail Queue Problem Illuminate\Mail with Lumen

Created on 30 Aug 2015  Â·  10Comments  Â·  Source: laravel/framework

I got fatal error: Call to a member function push() when run:

Mail::queue('email.blank', ['html' => $html], function($msg) {
            $msg->to(['[email protected]']);
            $msg->from(['[email protected]']);
            $msg->subject('Lumen Service Mail');
        });

Its work if change file: Illuminate\Mail\Mailer.php

Add
use \Illuminate\Support\Facades\Queue as Queue;

And change line 205:
from: return $this->queue->push('mailer@handleQueuedMessage', compact('view', 'data', 'callback'), $queue);
to return Queue::push('mailer@handleQueuedMessage', compact('view', 'data', 'callback'), $queue);

this work for me!

Most helpful comment

FYI @GrahamCampbell this is a bug. IMHO, all Illuminate elements should be made to work out of the box with Laravel and Lumen even if they are not explicitly included.

The best fix I've found for this (in Lumen 5.5) is to do the following:

In bootstrap/app.php, make sure the MailServiceProvider is listed before your AppServiceProvider, e.g.:

...
$app->register(Illuminate\Mail\MailServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
...

Then along the lines of @soderluk's comment on this thread, in your AppServiceProvider add the following in your register method:

app('mailer')->setQueue(app('queue'));

All 10 comments

That's NOT the correct solution.

All you need to do is make sure you've used the queue before you use mail. That's just due to the way Lumen doesn't even bother to bind stuff unless you try to use it, to save time. If you don't like that behaviour, I'd recommend just using Laravel.

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Mail/MailServiceProvider.php#L77

Thank you for your response.

Im using the queue only to send emails. Do you have any suggestion to start the queue before send email?

-----Mensagem Original-----
De: "Graham Campbell" [email protected]
Enviada em: ‎30/‎08/‎2015 04:54
Para: "laravel/framework" [email protected]
Cc: "Rodrigo Lacerda" r.[email protected]
Assunto: Re: [framework] Mail Queue Problem Illuminate\Mail with Lumen(#10083)

Closed #10083.
—
Reply to this email directly or view it on GitHub.

I have the same issue and I would also like to know how to start the queue before sending email.
This would also add a valid solution for this issue: https://github.com/laravel/lumen-framework/issues/77

Like graham said this is not the right solution. You can check the implementation of the Illuminate\Mail\Mailer class and you will see it has a setQueue method so by doing:

public function __construct()
{
    $this->mailer = app('mailer');
    $this->mailer->setQueue(app('queue')->connection('redis'));
}

It is done like that. You can even inject the Mailer or the QueueContract with bindings, however you want.

Connection refers on you queue.php key config of course.

Create a new job - use the follwoing sample code

namespace App\Jobs;
use App\Jobs\Job;
class SendEmail extends Job
{
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
    }
} 

and in your controller

include the SendMail under the namepsace;

use App\Jobs\SendEmail;

mail sending method

public function mailSend()
{
        $data = array(
            'title' => 'test title',
            'description' => 'test description',
        );
        Queue::push(new SendEmail); // or $this->dispatch(new SendEmail());
        Mail::queue('email.mytemplate', $data, function ($message) {
            $message->from('[email protected]', 'Ishan test mail server');
            $message->to('[email protected]')->subject('test subject');
        });
}

@GrahamCampbell if it's an intended behavior what is the right way to test mailables?

Looks like this problem was fixed in Lumen 5.5. The problem still exists.

@chimit The queue isn't being bounded in the mail service provider. So the setQueue isn't called properly.

I found a solution for 5.4's.

Look at here: https://github.com/laravel/lumen-framework/issues/590#issuecomment-316363236

FYI @GrahamCampbell this is a bug. IMHO, all Illuminate elements should be made to work out of the box with Laravel and Lumen even if they are not explicitly included.

The best fix I've found for this (in Lumen 5.5) is to do the following:

In bootstrap/app.php, make sure the MailServiceProvider is listed before your AppServiceProvider, e.g.:

...
$app->register(Illuminate\Mail\MailServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
...

Then along the lines of @soderluk's comment on this thread, in your AppServiceProvider add the following in your register method:

app('mailer')->setQueue(app('queue'));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  Â·  3Comments

klimentLambevski picture klimentLambevski  Â·  3Comments

iivanov2 picture iivanov2  Â·  3Comments

kerbylav picture kerbylav  Â·  3Comments

ghost picture ghost  Â·  3Comments