I am using Laravel 5.8, I configured an email containing the invoice sent to user after they place an order.
Email is delivered when set QUEUE_DRIVER=sync
but when I set it to redis/database and I run the php artisan queue:work redis --tries=5
it shows the queue is processed
Processing: ModulesCheckoutMailInvoice
Processed: ModulesCheckoutMailInvoice
succesfully and no entry in the failed_jobs either.
CODE:
ModulesCheckoutJobsSendInvoiceEmail.php
class SendInvoiceEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function handle()
{
Mail::to($this->order->customer_email)
->send(new Invoice($this->order));
}
}
And this is the ModulesCheckoutMailInvoice.php
class Invoice extends Mailable
{
use Queueable, SerializesModels;
public $order;
public function __construct($order)
{
$this->order = $order;
}
public function build()
{
return $this->subject('New order: ', ['id' => $this->order->id]))
->view("emails.invoice");
}
}
Then I am dispatching it in the Controller after Order created
SendInvoiceEmail::dispatch($order);
Can anyone point out if I’ve missed anything and what I am doing wrong?
I’ve done the php artisan config:cache and clear:cache
restarted the server.
This works just as expected if I set QUEUE_DRIVER=sync
in .env instead of QUEUE_DRIVER=redis or database
2
Answers
Thing is
.env
orconfig.php
both configurations should work normally. My main issue was the application would save these settings from.env
orconfig.php
to database. then for the next time it looked in database for these smtp configs and anything other thansync
tends to fail. I don't know why it was configured so I removed and the issue was resolvedI had that problem too and solve it putting a key in the
config/mail.php
filein
mailers['smtp']
I addlocal_domain
equals to my domain (example.com
)The reason is the Swift Mailer class can use this key to put in the HELO field of the email. If you use
QUEUE=sync
the framework uses the domain in env file, but when you use a different queue it doesn’t cache the domain.