Skip to content

Mail subject does not work when using queue:work #41240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
edsonfeimberg opened this issue Feb 24, 2022 · 3 comments
Closed

Mail subject does not work when using queue:work #41240

edsonfeimberg opened this issue Feb 24, 2022 · 3 comments

Comments

@edsonfeimberg
Copy link

edsonfeimberg commented Feb 24, 2022

  • Laravel Version: 8
  • PHP Version: 8
  • Database Driver & Version: mysql

Description:

I just experienced a problem where for no reason the subject of my emails was not being sent. Googling i found this resource:

https://stackoverflow.com/questions/61836901/laravel-mailable-subject-not-working-if-i-queue-the-mailable

Where it was stated that the problem could be solved by using queue:listen instead of queue:work. Is there an specific reason for why mail subject shouldn't work when using queue:work especially since it is known to have better performance than queue:listen?

@derekmd
Copy link
Contributor

derekmd commented Feb 25, 2022

Which mail driver? You'll need to share your code for the mailable class and the Mail facade call.

The below example shows the expected email subject line is correctly set for both commands queue:work and queue:listen.


App\Mail\Foo.php

namespace App\Mail;

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

class Foo extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->subject('foo')->view('mail.foo');
    }
}

resources/views/mail/foo.blade.php

bar

.env

MAIL_MAILER=log

Laravel Tinker

Mail::to('baz@example.com')->queue(new App\Mail\Foo);

command line

Run php artisan queue:work, then repeat the above step and run php artisan queue:listen.

storage/logs/laravel.log

This email header/body is logged using both commands.

From: Laravel <no-reply@example.com>
To: baz@example.com
Subject: foo
MIME-Version: 1.0
Date: Fri, 25 Feb 2022 01:00:18 +0000
Message-ID: <e804f4d5cab4d4661ab669c8b83cc03e@example.com>
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

bar  

Keep in mind if you're using Mail::fake() and Mail::assertSent()/assertQueued() while testing, the mailable instance build() method must manually be called to make assertions on the subject line.

use App\Mail\Foo;

Mail::fake();

Mail::to('baz@example.com')->queue(new Foo);

// this fails
Mail::assertQueued(Foo::class, function ($mail) {
    // build() hasn't been called so the subject isn't filled yet
    return $mail->subject === 'foo';
});

// this passes
Mail::assertQueued(Foo::class, function ($mail) {
    return $mail->build()->subject === 'foo';
});

@driesvints
Copy link
Member

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

@driesvints
Copy link
Member

Thanks @derekmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants