1
votes

There are many similar questions, the closest Laravel 6 Event Listener Mailable Queue unable to access.

php: 7.2
Laravel: 5.7

  1. Controller:
public function send_email(Request $request) {
    $data = $request->json()->all();
    //data: {"to":"[email protected]","msg":"test message","subject":"test subject"}

    Mail::to($data['to'])->send(new MailFromNewinc( $data['subject'], $data['msg']));

    return Response::json(array(
        'error' => false,
        'status_code' => 200
    ));
}
  1. MailFromNewinc Mailable:
namespace App\Mail;

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

class MailFromNewinc extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;
    public $msg;
    public $header;

    public function __construct($header, $msg)
    {
        $this->msg = $msg;
        $this->header = $header;
    }

    public function build()
    {
        return $this->subject($this->header)
                ->view('emails.sendmail_from_newinc');
    }
}
  1. sendmail_from_newinc view:
<body>
{{ $header??'no header' }}
<br>
{{ $msg??'no content' }}
</body>
</html>

Result:
test subject
no content

If I remove ShouldQueue implements from mailable $msg is accessible, but if I leave in, $msg is null. No matter if I set in builder ->with('msg', $this->msg);
$header is accessible in both ways.

UPDATE
If I change the variable name $header to something else (everywhere in the mailable and in the view) then it also stops working. Is it a reserved word?

If I leave in but change the subject :

return $this->subject('custom subject')->view('emails.sendmail_from_newinc');

the email still comes with subject defined in $header and not 'custom subject'. :O

I already try to render the mail in the controller:

return (new MailFromNewinc($data['subject'], $data['msg']))->render();

and it shows everything in order, all variables in place.

I don't know how I messed up, I have an another mailable, with almost same setting, but one $header and one custom model in the __construct function, and everything is accessible in its view as it should be.

1

1 Answers

1
votes

Ok, now I find the problem: I had to restart queue worker.
I don't really know how it is working, but something stuck in, because when I changed even the view in the mailable build function, it still sent with the previous view.
After queue restart everything works normal.