Xammie / mailbook

✉️ Laravel Mail Explorer
https://mailbook.dev
MIT License
443 stars 17 forks source link

Doesn't work with ShouldQueue contract on mailable #38

Closed joostdebruijn closed 1 year ago

joostdebruijn commented 1 year ago

It looks like this package doesn't work with the new style mailables as introduced in laravel/framework#44462. When using a new style mailable, the following error occurs: Xammie\Mailbook\ResolvedMail::__construct(): Argument #1 ($message) must be of type Symfony\Component\Mime\Email, null given.

You can test it by replacing App\Mail\MailbookMail by this:


<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

class MailbookMail extends Mailable
{
    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Welcome to mailbook!'
        );
    }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content(): Content
    {
        return new Content(
            markdown: $this->getView('mail.mailbook')
        );
    }
}
Xammie commented 1 year ago

Thanks for your report, I'm going to look into it.

Xammie commented 1 year ago

@joostdebruijn Your problem seems to be in the content function. The method getView() does not exist. The following works for me.

public function content(): Content
{
    return new Content(
-       markdown: $this->getView('mail.mailbook')
+       markdown: 'mail.mailbook'
    );
}

In case this does not work could you supply more context like OS, PHP version, Laravel version etc?

joostdebruijn commented 1 year ago

@Xammie Thanks for looking at this issue, you're right: the getView() function was not available in the example, it was just a copy/paste mistake from my side.

However, the issue still persists and I found out that the issue happens when using the ShouldQueue contract on the mailable. This is a tested and reproducable example of my issue ("Xammie\Mailbook\ResolvedMail::__construct(): Argument #1 ($message) must be of type Symfony\Component\Mime\Email, null given"):


<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

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

    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Welcome to mailbook!'
        );
    }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'mail.mailbook'
        );
    }
}

When removing implements ShouldQueue the viewer works again.

Xammie commented 1 year ago

Thanks again for reporting. This has been fixed in version 1.5.3.

daniel-de-wit commented 1 year ago

In my case the esign/laravel-email-whitelisting package caused the same error message. Even though it was disabled by setting EMAIL_WHITELISTING_ENABLED=false..

The event dispatcher caused a halt. Can't figure out why.

(i know this is not the concern of this wonderful package, but it might save other a few hours of debugging)