Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Zile's avatar
Level 25

Url helper function in email templates

I was using url() function in my email templates, and everything was working fine untill I switched to 'queue' instead of 'send'. Seems like url() function is returning 'http://localhost' when delayed (using crone jobs). I ended up passing my url-s as data from controllers, avoiding to call url() in my blade templates. So, is this expected behavior and, more important, is this the right way to insert links in queued email templates?

BTW, I'm totally new to PHP and Laravel, I am sorry if my question is stupid :)

0 likes
30 replies
paradox's avatar

Guessing - config/app.php has 'url' => 'http://localhost' ? As the comment in the file states you need to set that in order to have proper links when running from command line.

Zile's avatar
Level 25

@paradox Thanks for your time. It is the case. I haven't changed that line, so it is 'url' => 'http://localhost'... But I thought it wouldn't affect url() helper function which should return domain name and which is working fine in my blade templates when called synchronously (both localy and in production). Another thing, I definitely don't want to have the root of my application in my links; I want http://website.com/some-route instead... Either the documentation is misleading or I am that confused... Or both :-)

paradox's avatar

@Zile

I definitely don't want to have the root of my application in my links; I want http://website.com/some-route instead...

Actually I did't get it, sorry :( Can you please provide an example? Given url('some/route') you should get full qualified name: http://website.com/some/route

which should return domain name and which is working fine in my blade templates when called synchronously (both localy and in production)

It does though. When hit by a browser, Laravel got already a valid Illuminate\Http\Request object. When you run an Artisan command however Laravel must create a Request object which is done in SetRequestForConsole.php. And so it uses an url defined in the config/app.php. If it resolves your problem I appreciate accepting this solution :-)

Zile's avatar
Level 25

@paradox You asked for it :-)

I wanted my users to be confirmed by email, so I'm sending them an email with link, based on my template. Inside that template I generate href attributes with url('confirmation/'.$randomCode). When I use Mail::send, everything is working fine, and emails have correct links (like http://dev.example.com/confirmation/laWiu1df12oa2uigr3qjb...). But when I use Mail::queue - links in emails point to http://localhost/confirmation/laWiu1df12oa2uigr3qjb...

As far as I understand comments above 'url' => 'http://localhost' line, I should change this to path to artisan file, which is /var/www/html/example.com/ and which makes perfect sense to me (we should point task parser, or whatever it's name is, to correct location). But if I change that line to point to root of my app, my href attributes would look something like /var/www/html/example.com/confirmation/laWiu1df12oa2uigr3qjb... which makes no sense.

I just need fully qualified URL to the given path in my templates, but it looks like the different 'parser' is handling my call to url() function, or the same parser is returning different results based on some conditions, if you know what I mean :(

If I hardcode the value to, for example, 'url' => 'http://dev.example.com', what happens on production site?

As I said earlier, I ended up passing correct urls to my email templates from controllers, but it just feels strange to do that, like I am forbidden to use helper functions in blade templates in some situations, and allowed in some other situations...

And yes, I really appreciate your effort to clear this thing to me. Thanks.

paradox's avatar
paradox
Best Answer
Level 8

@Zile Ok, clearing up ;-) You should not pass physical location on your server like /var/www/html/example.com. Instead just update the config which reflects your production url. If you are afraid of mixing domains, don't worry. Laravel 5 solves that problem by reading variables from your .env file in root directory and converting those to environmental variables. That means you can put 'url' => env('APP_URL') in your config/app.php instead of http://localhost I suggested earlier and in your development machine then edit .env file by adding line APP_URL=http://localhost (or any name for that matter you use in development cycle) and when you release your app on remote server do the same changing APP_URL appropriately.
I believe that should do the trick. Emails should have full qualified URL on development and production sites - http://localhost/some/route and http://website.com/some/route respectively.

Further reading: http://laravel.com/docs/5.0/configuration#environment-configuration

And don't give up ;-)

2 likes
Zile's avatar
Level 25

@paradox I understand what you suggested, I did something similar with different EMAIL variables for local and remote machine, so I think I know how to do that :-)

Thanks for your help!!!

concentrico's avatar

@paradox I've set up my APP_URL in the .env file but it still uses localhost. I already did php artisan cache:clear, composer dump autoload, restarted nginx etc...

Do you know if there's anything else I need to do for the change to take effect?

Any suggestions is greatly appreciated.

paradox's avatar

@concentrico Hi again, sorry I've just seen your latest reply! I'm going to dig in it in a couple of hours (8 or 9) when I return home. We are going to nail it tonight, don't worry ;) In a meantime, is there any chance to see what you have ended with? Just important part of the code (configs, controller, templates) I don't need to see the whole app..

concentrico's avatar

@paradox I hadn´t seen the message, I greatly appreciate the help!! You won't believe that it was a typo in my env file that prevented me from doing this! I banged my head for hours and it was a ";"

Thank you very much!

subhramaiti's avatar

I am having the same issue. APP_URL is set. But still i am getting localhost in my email. Please help

akhileshdarjee's avatar

@paradox : Even I'm getting the same issue, I've set 'APP_URL' in env and in 'config/app.php', the 'url' variable is as follows:

'url' => env('APP_URL', 'http://localhost')

And I'm not using queue, QUEUE_DRIVER=sync. I've cleared cache and config. But still getting localhost url in email. Please help

garnerv's avatar

@zshanjun Thank you SO much for your response. I just spent a couple of hours trying to figure out why my mail queue worker was prefixing all of my URLs with "http://localhost" when this behavior was not happening when the mails were done synchronously.

razamoh's avatar

Hi All , Follow this to fix this issue .

1.SET UR APP_URL= yourdomain.com

2.run php artisan config:cache

3.php artisan queue:restart

Most Probably it will fix the issue.

Please or to participate in this conversation.