php - Laravel Email in Job Queue not working except sync
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: Modules\Checkout\Mail\Invoice
Processed: Modules\Checkout\Mail\Invoice
succesfully and no entry in the failed_jobs either.
CODE: Modules\Checkout\Jobs\SendInvoiceEmail.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 Modules\Checkout\Mail\Invoice.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
Answer
Solution:
I had that problem too and solve it putting a key in the config/mail.php file
in mailers['smtp'] I add local_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.
Answer
Solution:
Thing is .env or config.php both configurations should work normally.
My main issue was the application would save these settings from .env or config.php to database. then for the next time it looked in database for these smtp configs and anything other than sync tends to fail.
I don't know why it was configured so I removed and the issue was resolved