Skip to content

Artisan queue:work command does not clear assignments in mailables #86

@linushstge

Description

@linushstge

In Version 6 and 7 there is an issue with assigned template variables in relation with the queue:work command.
Instead of queue:listen queue:work does not always boot Laravel so the existing Smarty Instance is kept.

Steps to reproduce

  1. Create two Mailables
  2. Create a simple smarty template where the variable is dumped
  3. Assign a variable only to the first mailable view
  4. Queue both Mails

Result in queue:listen: As expected, the variable gets only applied to the first mailable
Result in queue:work: The variable is dumped in both templates

Example files

App/Mail/TestMail1.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;

class TestMail1 extends Mailable
{
    use Queueable;

    public function build()
    {
        $this->view('mail.test', [
            'example' => 'assigned to first mailable'
        ]);

        return $this;
    }
}

App/Mail/TestMail2.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;

class TestMail2 extends Mailable
{
    use Queueable;

    public function build()
    {
        // "example" is not applied here
        $this->view('mail.test');

        return $this;
    }
}

templates/mail/test.tpl

<pre>{$example}</pre>

TestController.php

Mail::queue(new TestMail1());
Mail::queue(new TestMail2());

Required Changes

Smarty assignments have to be cleared on each Mailable.

In the current implementation, there is a risk that variables that are not overwritten by a second Mailable are erroneously transmitted in an email.

Notes

  • Tested with Laravel 9 and 10.
  • Smarty Caching false
  • Smarty Force Compile true
  • The test $variable is applied to all further mailables (even if the template file name differs)
  • This issue only occurs with Smarty templates, Blade templates are not affected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions