mailbook icon indicating copy to clipboard operation
mailbook copied to clipboard

ResolvedMail invoked with null causing error

Open joostdebruijn opened this issue 10 months ago • 4 comments

Hi,

I'm running Mailbook v1.8.0 with Laravel 11.2.0 and when visiting /mailbook I get the following error:

Xammie\Mailbook\Data\ResolvedMail::__construct(): Argument #1 ($message) must be of type Symfony\Component\Mime\Email, null given, called in /mnt/HC_Volume_38038656/home/timeteam/repositories/entries/vendor/xammie/mailbook/src/MailableSender.php on line 36

I didn't make any changes to Mailbook in the upgrade from Laravel v10 and I cannot find any documented breaking change in the changelog. It's not very clear to me what I can do to fix this.

joostdebruijn avatar Apr 09 '24 07:04 joostdebruijn

I will look into this. Which version of mailbook did you use before the upgrade to laravel 11?

Xammie avatar Apr 09 '24 07:04 Xammie

@Xammie Thanks! I was using version v1.7.0 of Mailbook before.

joostdebruijn avatar Apr 09 '24 07:04 joostdebruijn

I've been unable to reproduce your issue. It should have something to do with mailbook being unable to inject a mail driver which would result in failing to retrieve the message.

  • Can you send me the contents of your config/mail.php and output of php artisan config:show mail (please remove any sensitive data)
  • Any other information that could be of help like PHP version, OS, etc.
  • Do you get this issue on a fresh install?
composer create-project laravel/laravel mailbook-issue-79
cd mailbook-issue-79
composer require xammie/mailbook
php artisan mailbook:install
php artisan serve

Xammie avatar Apr 09 '24 10:04 Xammie

Also, does this happen for every mail you have registered or only for specific ones?

Xammie avatar Apr 09 '24 10:04 Xammie

@Xammie Thanks for your questions. It triggered me to play around again with some more tests and it looks like the problem relates specific to mails on which I call $this->afterCommit() in the constructor (all those emails implement the Illuminate\Bus\Queueable trait). Because the first registered email was using this, I was not able to access the /mailbook-URL, but it works as long as the emails don't have $this->afterCommit().

It's PHP 8.3.4, Laravel 11.3.0, Mailbook 1.8.0 at Linux 6.5.0-26 (Ubuntu)

joostdebruijn avatar Apr 09 '24 19:04 joostdebruijn

That sounds like a very possible cause. It's strange that this has not been an issue on Laravel 10. I will have a look at fixing this tomorrow. Thanks for the information.

Xammie avatar Apr 09 '24 19:04 Xammie

Hm, I'm not sure if it's not an issue in Laravel 10. At least, it wasn't an issue with Laravel 10 and Mailbook 1.7, but it might be with Mailbook 1.8, haven't tested that combination.

joostdebruijn avatar Apr 09 '24 20:04 joostdebruijn

I've sadly not been able to reproduce it with the following example

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

    public function __construct()
    {
        $this->afterCommit();
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Test Mail',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'view.name',
        );
    }
}

Is there anything I might be missing?

Xammie avatar Apr 10 '24 07:04 Xammie

I've this:


<?php

namespace App\Mail;

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

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

    /**
     * Create a new message instance.
     */
    public function __construct()
    {
        $this->afterCommit();
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address('[email protected]', 'Mailbook'),
            subject: 'Welcome to Mailbook!',
        );
    }

    public function content(): Content
    {
        return new Content(
            markdown: 'mail.mailbook',
        );
    }
}

If I remove either $this->afterCommit() or implements ShouldQueue it works, otherwise I get the error mentioned earlier.

joostdebruijn avatar Apr 10 '24 08:04 joostdebruijn

Can you see if it works when you put QUEUE_CONNECTION=sync in your .env. Also your config/queue.php file should start with:

return [
    'default' => env('QUEUE_CONNECTION', 'database'),
    'connections' => [
        'sync' => [
            'driver' => 'sync',
        ],
    ...

It should be totally fine to use ShouldQueue and a different queue driver than sync, but it seems that for some reason mailbook cannot temporarily change the queue driver when rendering the mail

Xammie avatar Apr 10 '24 08:04 Xammie

Yes, I was playing around with that too, however with QUEUE_CONNECTION=sync I still get the error.

php artisan config:show queue:

  queue .....................................................................................................................................  
  default .............................................................................................................................. sync  
  connections ⇁ sync ⇁ driver .......................................................................................................... sync  
  connections ⇁ database ⇁ driver .................................................................................................. database  
  connections ⇁ database ⇁ connection .................................................................................................. null  
  connections ⇁ database ⇁ table ....................................................................................................... jobs  
  connections ⇁ database ⇁ queue .................................................................................................... default  
  connections ⇁ database ⇁ retry_after ................................................................................................... 90  
  connections ⇁ database ⇁ after_commit ............................................................................................... false  
  connections ⇁ beanstalkd ⇁ driver .............................................................................................. beanstalkd  
  connections ⇁ beanstalkd ⇁ host ................................................................................................. localhost  
  connections ⇁ beanstalkd ⇁ queue .................................................................................................. default  
  connections ⇁ beanstalkd ⇁ retry_after ................................................................................................. 90  
  connections ⇁ beanstalkd ⇁ block_for .................................................................................................... 0  
  connections ⇁ beanstalkd ⇁ after_commit ............................................................................................. false  
  connections ⇁ sqs ⇁ driver ............................................................................................................ sqs  
  connections ⇁ sqs ⇁ prefix ............................................................ https://sqs.us-east-1.amazonaws.com/your-account-id  
  connections ⇁ sqs ⇁ queue ......................................................................................................... default  
  connections ⇁ sqs ⇁ suffix ........................................................................................................... null  
  connections ⇁ sqs ⇁ region ...................................................................................................... eu-west-1  
  connections ⇁ sqs ⇁ after_commit .................................................................................................... false  
  connections ⇁ redis ⇁ driver ........................................................................................................ redis  
  connections ⇁ redis ⇁ connection .................................................................................................. default  
  connections ⇁ redis ⇁ queue ....................................................................................................... default  
  connections ⇁ redis ⇁ retry_after ...................................................................................................... 90  
  connections ⇁ redis ⇁ block_for ...................................................................................................... null  
  connections ⇁ redis ⇁ after_commit .................................................................................................. false  
  batching ⇁ database ................................................................................................................. mysql  
  batching ⇁ table .............................................................................................................. job_batches  
  failed ⇁ driver ............................................................................................................ database-uuids  
  failed ⇁ database ................................................................................................................... mysql  
  failed ⇁ table ................................................................................................................ failed_jobs 

joostdebruijn avatar Apr 10 '24 09:04 joostdebruijn

Can you try out the following branch to see if it fixes your issue. Still have not managed to reproduce the issue but maybe this will resolve it.

composer require --dev xammie/mailbook dev-fix-after-commit

Xammie avatar Apr 10 '24 12:04 Xammie

Yep, running Mailbook with this branch solves the problem! Thanks!

joostdebruijn avatar Apr 10 '24 15:04 joostdebruijn

Awesome! I will release this soon.

Xammie avatar Apr 10 '24 15:04 Xammie

Fix has been released in mailbook 1.8.1

And thank you so much for sponsoring! 😃

Xammie avatar Apr 10 '24 17:04 Xammie