snowfire / Beautymail

Send beautiful HTML emails with Laravel
1.24k stars 207 forks source link

InvalidArgumentException: Only mailables may be queued. #85

Closed ediblemanager closed 3 years ago

ediblemanager commented 6 years ago

I get the feeling I'm missing something: Beautymail is a drop-in replacement for Mail, isn't it? So queueing should work without any issues.

This is Laravel 5.5 running on Ubuntu 14.04, PHP 7.0.

When running $beautymail->queue('emails.welcome', [], new NewUser($member));, (NewUser being a mailable, which implements ShouldQueue), I get the error InvalidArgumentException: Only mailables may be queued.

Swapping out Beautymail for Mail fixes the issue.

Mailer class:

namespace App\Mail;

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

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

    public $member;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('noreply@address.com')
            ->to($this->member->email)
            ->view('emails.welcome')
            ->text('emails.welcome_plain')
            ->with(
                [
                    'testOne' => '1',
                    'testTwo' => '2',
                ])
            ->attach('/var/git_repos/folder/account_logos_test/2_logo_medium.png', [
                    'as' => '2_logo_medium.png',
                    'mime' => 'image/png',
            ]);
    }
}

Stack trace:

InvalidArgumentException: Only mailables may be queued. in file /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php on line 354
Stack trace:
  1. InvalidArgumentException->() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:354
  2. Illuminate\Mail\Mailer->queue() /var/www/folder/releases/ba5a5f0c/vendor/snowfire/beautymail/src/Snowfire/Beautymail/Beautymail.php:90
  3. Snowfire\Beautymail\Beautymail->queue() /var/www/folder/releases/ba5a5f0c/app/Models/Member.php:542
  4. App\Models\Member->sendEmail() /var/www/folder/releases/ba5a5f0c/app/Http/Controllers/Api/EmailController.php:41
  5. App\Http\Controllers\Api\EmailController->send() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
  6. call_user_func_array() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
  7. Illuminate\Routing\Controller->callAction() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
  8. Illuminate\Routing\ControllerDispatcher->dispatch() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
  9. Illuminate\Routing\Route->runController() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169
 10. Illuminate\Routing\Route->run() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
 11. Illuminate\Routing\Router->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 12. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/app/Http/Middleware/AdminCheckRole.php:24
 13. App\Http\Middleware\AdminCheckRole->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 14. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 15. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/chrisbjr/api-guard/src/Http/Middleware/AuthenticateApiKey.php:51
 16. Chrisbjr\ApiGuard\Http\Middleware\AuthenticateApiKey->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 17. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 18. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
 19. Illuminate\Routing\Middleware\SubstituteBindings->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 20. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 21. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:57
 22. Illuminate\Routing\Middleware\ThrottleRequests->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 23. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 24. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
 25. Illuminate\Pipeline\Pipeline->then() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Router.php:660
 26. Illuminate\Routing\Router->runRouteWithinStack() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Router.php:635
 27. Illuminate\Routing\Router->runRoute() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Router.php:601
 28. Illuminate\Routing\Router->dispatchToRoute() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Router.php:590
 29. Illuminate\Routing\Router->dispatch() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
 30. Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 31. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/fideloper/proxy/src/TrustProxies.php:56
 32. Fideloper\Proxy\TrustProxies->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 33. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 34. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30
 35. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 36. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 37. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:30
 38. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 39. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 40. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
 41. Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 42. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 43. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:46
 44. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:149
 45. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 46. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
 47. Illuminate\Pipeline\Pipeline->then() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
 48. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() /var/www/folder/releases/ba5a5f0c/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
 49. Illuminate\Foundation\Http\Kernel->handle() /var/www/folder/releases/ba5a5f0c/public/index.php:55    
christianschoenmakers commented 6 years ago

After some time I figured out how to work with both Beautymail and the default Laraval Mailer + Queueing.

The Mailer Class you've created above is fine.

You should call it with the default method: Mail::to($reciever_mail)->queue(new Newuser($member));

This method will queue the mail and send it with your queue walker. In my case I've chosen the 'Sunny' template which returns errors about missing variables. I've solved it with adding the Beautymailer config variables to the view:

/**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('E-mail subject')
                    ->view('emails.viewname', config('beautymail.view'));
    }

I agree this one is a bit confusing and it took me some time to figure it out. Readme should be improved with documentation about queueing.

emilsundberg commented 6 years ago

@wespiremedia Great debug for solving the queue issue. I will happily accept a PR with readme changes regarding this.

(I'm not using queues with Beautymail)

andrei2424 commented 5 years ago

`Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to Illuminate\Mail\Mailable::view() must be of the type array, null given, called in /var/www/app/Mail/SendMessage.php on line 32 in /var/www/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php:687 Stack trace:

0 /var/www/app/Mail/SendMessage.php(32): Illuminate\Mail\Mailable->view('emails.welcome', NULL)

1 [internal function]: App\Mail\SendMessage->build()

2 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)

3 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container{closure}()

4 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))

5 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(572): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)

6 /var/www/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(151): Illuminate\Container\Container->call(Array)

7 /var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\Mail\Mailable->Illuminate\Mail{closure}()

8 /var/www/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(160): Illuminate\Mail\Mailable->withLocale(NULL, Object(Closure))

9 /var/www/vendor/laravel/framework/src/Illuminate/Mail/SendQueuedMailable.php(52): Illuminate\Mail\Mailable->send(Object(Illuminate\Mail\Mailer))

10 [internal function]: Illuminate\Mail\SendQueuedMailable->handle(Object(Illuminate\Mail\Mailer))

11 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)

12 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container{closure}()

13 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))`

According to the instructions above :(

andrei2424 commented 5 years ago

`class SendMessage extends Mailable implements ShouldQueue { use Queueable, SerializesModels;

public $user;

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

public function build()
{
    return $this
        ->from(config('mail.from.address'))
        ->to($this->user->email)
        ->subject('DB Design')
        ->view('emails.welcome', config('beautymail.view'));
}

}`

christianschoenmakers commented 5 years ago

Based on the error, config('beautymail.view') returns 'null' instead of a config array.

Does the config/beautymail.php file exists? Otherwise you should include them from the repo.

iamtoddperkins commented 5 years ago

Just an FYI for anyone else trying to figure this out. Just doing the below seems to have gotten everything to work for me, keeping Mailable.

class ReportMail extends Mailable
{
    public function build()
    {
        // Do stuff...

        $beautymail = app()->make(Beautymail::class);

        return $this->view('emails.report', $beautymail->getData());
    }
}

Note instead of passing in config('beautymail.view'), you should pass $beautymail->getData(). That way Beautymail is able to set the css and correct logo path.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.