Laravel: How to access config/Mail variables - php

I need to get address and name variables on my Mail.php config:
'from' => ['address' => env('MAIL_USERNAME'), 'name' => env('MAIL_NAME')],
I tried this:
Config::get('mail.from.*.name');
Config::get('mail.from["name"]');
But it doesn't work, what do I do to get them? Thanks.

I think you can directly access MAIL_NAME from .env
env('MAIL_NAME');
OR
Config::get('mail.from.name');
Hope this work for you !!!

this is the shortest method i know using helper methods :
config('mail.from.name')
or you can access it using your .env file with env('MAIL_NAME')

it is preferable put your mail name in your configuration file and access it with env function like this :
env('MAIL_NAME')

Related

Laravel not sending email to environment variable

I am using the following working code in Laravel:
Mail::to('mail#example.com')->send(new \App\Mail\CheckinInfo([
'client' => $client,
]));
when I use an email address from the environment, it stopped working:
Mail::to(env('MAIL_FROM_ADDRESS'))->send(new \App\Mail\CheckinInfo([
'client' => $client,
]));
In .env I got:
MAIL_FROM_ADDRESS=mail#example.com
and I tried
MAIL_FROM_ADDRESS="mail#example.com"
The cache and config are cleared.
The error message is:
[2022-07-29 18:44:26] production.ERROR: An email must have a "To", "Cc", or "Bcc" header. {"exception":"[object] (Symfony\\Component\\Mime\\Exception\\LogicException(code: 0): An email must have a \"To\", \"Cc\", or \"Bcc\" header. at /www/htdocs/app/laravel/vendor/symfony/mime/Message.php:128)
As a note when you do a config:cache the settings will not be grabbed from your .env anymore.
You should create a config/settings.php or similar file and store your env vars there, ie:
config/settings.php
return [
'mail_from_address' => env('MAIL_FROM_ADDRESS'),
]
and you should reference it as so
config('settings.mail_from_address')
No your mistake on the first line, maybe you didn't close the bracket
Try this way
Mail::to(env('MAIL_FROM_ADDRESS'))->send(new \App\Mail\CheckinInfo([
'client' => $client,
])); //Notice I added the closing parenthesis for To()
You are trying to access the functions of the env methods. This is not possible
I hope this is just the problem
You can see the documentation for the framework,
about this queueing-mail laravel documentation

How to define constants that can be used across config files in Laravel 5?

As I checked on multiple sources, the Laravel way of defining constant variables is through config files. However, in my case, I want to use constants across my config files which is not possible since, as I read, it's not possible/advisable to call a config from another.
EXAMPLE
Constants:
define('THE_ID_OF_SOMETHING_NICE', 1);
define('THE_ID_OF_SOMETHING_UGLY', 2);
define('THE_ID_OF_SOMETHING_BAD', 12372);
config1.php
return [
THE_ID_OF_SOMETHING_NICE = ['many', 'nice', 'data'],
]
config2.php
return [
['many', 'nice', 'data', THE_ID_OF_SOMETHING_NICE],
]
As you can see, I just can't use the actual value of the defined constants since it'll be too unreadable. Also I don't want to clump up my .env file with these constants since it's not really meant for it.
Any workaround for this? Thanks.
P.S
Why does this so hard whilst it should be very basic principle of PHP to utilise constant definitions. CI has these figured out :/
I think you can use php include on your config.php if you don't want add a lot of to .env file. Just like that
<?php
include 'something_constant.php';
return [
THE_ID_OF_SOMETHING_NICE = ['many', 'nice', 'data'],
];
The best way to do this is to add them to the config/app.php file. Somewhere above the providers[] list you could add
'CONST' => ['THE_ID_OF_SOMETHING_NICE' => 1,
'THE_ID_OF_SOMETHING_UGLY' => 2,
],
and anywhere in the code access the values with the laravel helper config('app.CONST.THE_ID_OF_SOMETHING_NICE');
You can make use of the .env file but beware that in production, this file is ignored because the config is automatically cached. In .env you could add the line THE_ID_OF_SOMETHING_NICE=1 and in the config/app.php file you add
'CONST' => ['THE_ID_OF_SOMETHING_NICE' => env('THE_ID_OF_SOMETHING_NICE'),],
from where you can access the value just the same with the config() helper.
Personally I add the values in the app.php file and don't bother with adding values to .env, because mostly these contain non-critical information (i.e. your private keys etc)
If you'd like to create a separate file to isolate from the other config files, you could create a file f.ex. config/constants.php and return an array as it is done in other config files. Make it a flat array (no 'CONST' key). In the app/providers/AppServiceProvider in the register() method add
$this->mergeConfigFrom('config/constants.php', 'constants');
This way you can access your constants with config('constants.THE_ID_OF_SOMETHING_NICE');

how to access variable from config file in laravel

I have one file inside config folder lets say: file1
File Location:
config/
---file1.php
I have following code in my file: file1.php
return [
'MASTER_KEY' => ['SUB_KEY1' => 'SOME_VALUE', 'SUB_KEY2' => 'SOME_VALUE', 'SUB_KEY3' => 'SOME_VALUE'],
];
How to access the value from MASTER_KEY of particular SUB_KEY?
Assuming for SUB_KEY2, Try -
config('file1.MASTER_KEY.SUB_KEY2')
Guide
In order to access that value you have 2 choices which are the same at the end:
first one: which use Config refers to the config facade.
use Config;
$myValue = Config::get('file1.MASTER_KEY.SUB_KEY1');
enter code here
second one: using config() helper function which uses Config facade and its only an alternative and easy way to use Config facade.
$myValue = config('file1.MASTER_KEY.SUB_KEY1');
use
config('file1.keyname');
if you have made any changes in config file then it might not work . so after changes in config file you have to run the following two commands .
php artisan config:cache
php artisan config:clear
Make a use Config; in your controller.
In config/file1.php
<?php return [
'ADMIN_NAME' => 'administrator',
'EMP_IMG_PATH' => '../uploads/profile/original',
'EMP_DOC_PATH' => '../uploads/profile/documents', ];
In controller
use Config;
$variable= Config::get('file1.EMP_DOC_PATH');
By this way you can get the variables value from config.
I think this will make something good.

Laravel 5 accessing global variable in mail.php

I need to set admin email in many place. so I created constants.php in config folder.
<?php
return array(
'admin_email' =>'joe#doe.com',
'admin_name' =>'Admin',
);
I was able to access this in my routes.php
dd(Config::get('constants.admin_email'));
However, when I try to access it in mail.php by
'from' => [
'address' => Config::get('constants.admin_email'),
'name' => Config::get('constants.admin_name')
],
I got Class 'Config' not found in mail.php.
Any suggestions? Thanks.
After some testing, I've found you can't use Config, \Config or config() in any files in your config folder. I believe they are not available to any of these files, but I'm not 100% sure why this is.
Regardless, to solve this issue and still have them available in other parts of your application, use env or environment variables. In your .env file, add the following:
ADMIN_EMAIL=joe#doe.com
ADMIN_NAME=Admin
Then, in your mail.php and anywhere else you want to use them, access them using:
'from' => [
'address' => env('ADMIN_EMAIL'),
'name' => env('ADMIN_NAME')
],
You can actually see them already in use in your mail.php and other config files, so it makes sense to use what already works. Hope that helps!
Use
config('constants.admin_email');

Laravel 5.1 GuzzleHttp Client error: 404 when using mailgun

I'm using the Mailgun service when sending mail with Laravel. However, i've set this up today and it's just stopped working. I have entered all the correct info in .env, config/services.php and config/mail.php. However i'm still getting the below error:
ClientException in Middleware.php line 69:
Client error: 404
It looks like the domain is not getting passed through somehow, even though in my config/services.php file I have:
'mailgun' => [
'domain' => env('mydomain.com'),
'secret' => env('<my-mailgun-key>'),
],
I have hidden the above credentials for safety, but in my real application they are the proper values.
Please help.
I was having a very similar 404 issue and tried the solution mentioned by Rogério. I thought I was doing it right, but gave it a try anyway. But misuse of the env() function wasn't my problem.
I set the config/services.php back to look like so:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN',''),
'secret' => env('MAILGUN_SECRET',''),
],
This will provide empty strings if the values named MAILGUN_DOMAIN and MAILGUN_SECRET are not found in the .env file. Then, in my .env file, I included the API Base URL and API Key from the Mailgun domain information page. So the .env looked something like this:
MAILGUN_DOMAIN=https://api.mailgun.net/v3/sandbox123abc.mailgun.org
MAILGUN_SECRET=key-123456abcdef
The values were passing along as they should, but still 404. Looking at How do I start sending email documentation at Mailgun, I saw that their API URL included "messages" on the end - which I tried manually adding to the .env setting:
MAILGUN_DOMAIN=https://api.mailgun.net/v3/sandbox123abc.mailgun.org/messages
That didn't work either, but lead me to look more carefully at the stack trace spit out by Laravel. That's when I noticed that the URL it was trying to connect with was:
https://api.mailgun.net/v3https://api.mailgun.net/v3/sandbox123abc.mailgun.org/messages/messages.mime
Ah-ha! Using Mailgun's Base API URL was incorrect! That was made obvious by the repetition of the "https://api.mailgun.net/v3" portion. So the aptly named MAILGUN_DOMAIN setting really just needed to be:
MAILGUN_DOMAIN=sandbox123abc.mailgun.org
Seems obvious now, but I spent way too much time figuring it out. Thought I'd put it out there in case anyone else happened to miss that detail
I hade the same problem as you and solved it by removing the env() call.
Thats because env will return the value of the env variable in the first argument (not the value of the argument) and otherwise return the second argument.
So:
'mailgun' => [
'domain' => 'mydomain.com',
'secret' => '<my-mailgun-key>',
],
Try that.

Categories