laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

Queued mails problem #859

Closed chimit closed 5 years ago

chimit commented 5 years ago

Description:

Queued mails don't work:

[2018-12-07 06:08:19] local.ERROR: Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Mail\Mailer] is not instantiable while building [Illuminate\Notifications\Channels\MailChannel]. in ../vendor/illuminate/container/Container.php:945

Steps To Reproduce:

Put a Mailable or Mail Notification into the queue.

Additional info

Have to rise this problem again as it still takes place in the latest version of Lumen.

The problem can be bypassed by adding:

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

into the bootstrap/app.php file, but @taylorotwell said it should be solved in the framework itself.

Please, don't close this issue until it's solved.

Related issues:

https://github.com/laravel/lumen-framework/issues/503 https://github.com/laravel/lumen-framework/issues/590 https://github.com/laravel/lumen-framework/issues/77 https://github.com/laravel/lumen-framework/issues/221 https://github.com/laravel/lumen-framework/issues/634

driesvints commented 5 years ago

You're sure you've followed all steps from https://lumen.laravel.com/docs/5.7/mail ?

I'll need more info and/or code to debug this further. Please post relevant code like models, jobs, commands, notifications, events, listeners, controller methods, routes, etc. You may use https://paste.laravel.io to post larger snippets or just reply with shorter code snippets. Thanks!

chimit commented 5 years ago

Yes (I wrote it).

Mail is not sent when is queued in any cases. It's simple to reproduce:

// User.php

namespace Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;
// Notifications/SendVerificationEmail.php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class SendVerificationEmail extends Notification implements ShouldQueue
{
    use Queueable;
// UserController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Auth;
use App\User;
use App\Notifications\SendVerificationEmail;

class UserController extends ApiController
{
    public function create(Request $request)
    {
        // ...

        $user->notify(new SendVerificationEmail);

And try to run it through the queue:

php artisan queue:work

Queue task will fail. Logs:

[2018-12-07 06:08:19] local.ERROR: Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Mail\Mailer] is not instantiable while building [Illuminate\Notifications\Channels\MailChannel]. in ../vendor/illuminate/container/Container.php:945
driesvints commented 5 years ago

Did some digging into this and I believe you always will have to do this. In Laravel it's done for you in the registerCoreContainerAliases method of the Application class:

https://github.com/laravel/framework/blob/3955e991fd465f16bfcdee816b4a614baa078617/src/Illuminate/Foundation/Application.php#L1099

But because the mailer component doesn't comes included with Lumen this isn't done in Lumen's Application class. It seems it's best to add this to the Lumen docs itself. I'll send in a PR.

driesvints commented 5 years ago

Sent in a PR: https://github.com/laravel/lumen-docs/pull/162

hakuno commented 5 years ago

Finally I've seen it solved. Thanks, @driesvints . Great job!