skip to Main Content

I always have lots of problems with Mail::queue and this time the subject is not being applied properly.

This is my class:

<?php

namespace AppMail;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

class PlanExpiringOrExpired extends Mailable
{
    use Queueable, SerializesModels;

    private $payment = null;


    public function __construct($payment)
    {
        $this->payment = $payment;
        $this->subject($this->payment->subject);

        Log::debug("Subject: {$this->payment->subject}");
    }

    public function build()
    {
        $this->to($this->payment->email, $this->payment->name)
            ->view('mails/payment')
            ->with('payment', $this->payment);

        return $this;
    }
}

And I call it this way:

$payment = AppModelsPayments::findOrFail($id);
$payment->subject = 'Your account has been canceled';

Mail::queue(new AppMailPlanExpiringOrExpired($payment));

The log saved correctly the following content:

[2023-02-12 11:00:04] local.DEBUG: Subject: Your account has been canceled

Yet the user received as subject: Plan Expiring or Expired (which is basically the class name).

Since I’ve done this change recently, do you think this might be a cache-related problem? If so, I’m using Supervisor to run queues, how do I clear the cache (through PHP) without messing up the production server?

I have used in the past something like this.

Artisan::call('cache:clear');

But I’m not sure if this is correct, or if it has any implications for my production server.

3

Answers


  1. Chosen as BEST ANSWER

    Solved.

    It was indeed a cache problem, it is also necessary to restart the queue. My solution was to create a private endpoint like /superadmin/clear-cache and use it whenever I need.

    Route::get('/superadmin/clear-cache', function()
    {
        Artisan::call('cache:clear');
        Artisan::call('queue:restart');
    });
    

  2. Have you tried it this way to setup the proper subject?

    private $payment = null;
    
    public function __construct($payment)
    {
        $this->payment = $payment;
    }
    
    public function build()
    {
        $this->to($this->payment->email, $this->payment->name)
            ->subject($this->payment->subject)
            ->view('mails/payment')
            ->with('payment', $this->payment);
    
        Log::debug("Subject: {$this->payment->subject}");
    
        return $this;
    }
    

    Move the subject set into build

    Login or Signup to reply.
  3. iam doing like this in queue class, EmailContactForm is a mailable class.

        public function handle()
    {
        $email = new EmailContactForm([
            'locale' => $this->data['locale'],
            'from_email' => $this->data['from_email'],
            'name' => $this->data['name'],
            'topic' => $this->data['topic'],
            'subject' => $this->data['subject'],
            'msg' => $this->data['msg']
        ]);
    
        Mail::to($this->data['to_email'])
            ->bcc(config('app.mail_from_address'))
            ->send($email);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search