2

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 to database
  • 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?

Community
  • 1
  • 1
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • I know it's stupid. But how about `env('APP_URL').'/images/'.$purchase->image` p.s. it's just workaround, maybe for commandline executions it needs different env file? Can You check `asset` method's code? – num8er Jan 26 '17 at 09:57
  • In fact this issue still exist: https://github.com/laravel/framework/issues/14139 – num8er Jan 26 '17 at 10:03
  • Thanks @num8er, my Googling didn't turn up that issue. Let me check it out a bit more ... yes I can workaround, in fact maybe that's all I can do. It is strange that it is not more common though, surely I am not the only person using `asset()` in queued emails? – Don't Panic Jan 27 '17 at 00:53
  • 2
    I think this problem still not fixed. In my custom artisan console commands I always user exact paths, urls and etc. because of this issue that never was working as is. – num8er Jan 27 '17 at 00:55
  • 1
    Thanks, guess that's what I have to do ... thanks for you help! – Don't Panic Jan 27 '17 at 01:50
  • 1
    @num8er I know it is late but your comment is really the answer - it is a bug. If you want to repost it as an answer I'd be happy to accept it! :-) – Don't Panic Apr 11 '17 at 09:00
  • 1
    Thanks) I'm not hunting for upvotes) just trying to be helpful. Just summarize Your solution and answer to You own question with solution, to be helpful to ones who came here – num8er Apr 11 '17 at 09:04

1 Answers1

0

Thanks to @num8er for finding the solution.

This is a known problem in Laravel that comes from Symfony: https://github.com/laravel/framework/issues/14139

A workaround is to do as @num8er suggested in the comments, and hard-code asset paths in views, rather than use asset():

config('app.url') . '/images/' . $purchase->image
Don't Panic
  • 13,965
  • 5
  • 32
  • 51