I have a Laravel 5.2 app which sends a few emails when a user buys a product. The email view includes references to some images, like so:
<img src="{{ asset($purchase->image) }}">
This works fine in all 3 environments I have - local, staging, and production. asset()
correctly constructs the fully qualified URLs to the appropriate image, using the configured APP_URLs in each environment.
I decided to switch to using Laravel queues to send the emails.
- I changed the
QUEUE_DRIVER
in.env
todatabase
php artisan queue:table
php artisan migrate
php artisan queue:listen
Changed
\Illuminate\Support\Facades\Mail::send(
to
\Illuminate\Support\Facades\Mail::queue(
and made a test purchase. The process works, the mail is sent, but the image URLs in the delivered emails are wrong. It seems that my configured APP_URL is not being picked up.
.env
APP_URL=http://localhost/path/to/app
config/app.php
'url' => env('APP_URL', 'http://localhost'),
The URLs asset()
generates in my email are:
http://localhost/images/foo.jpg
which is incorrect, they should be:
http://localhost/path/to/app/images/foo.jpg
It looks like using queues the APP_URL
defined in my .env
is not seen, so the default of http://localhost
is used. Not using queues, the same code works fine.
The only thing I can think of is that the CLI PHP environment which is handling the queue is somehow different from the Apache PHP environment, but I can't imagine what difference would cause .env
to be ignored.
I found a similar question from someone using Laravel 4.2, 2 years ago, with no answer. I found a few other similar references but no solution. Anyone seen this or have any suggestions?