How can I access my .env email variable via controller - laravel

I have quite a few emails that must go to the administrator of the site, which is the email declared in the .env
So, in each mailable that I create, for the $this->to(), I want to add the email defined in the .env so that in case it changes, I dont have to go around each mail and manually change it.
So I tried this just for testing purposes:
Route::get('send', function(){
Mail::send(new AdminEmail());
})->name('test.email');
The Mailable looks like this in the constructor:
public function __construct()
{
$this->from('no-replya#gmail.com', 'Tester');
$this->to(env("MAIL_USERNAME"), 'Admin');
}
In the .env I have the configurations:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=admin#gmail.com
MAIL_PASSWORD=z6c4czc44
MAIL_ENCRYPTION=tls
When I try to send the email I see the following error:
Address in mailbox given [] does not comply with RFC 2822, 3.6.2.
So it seems like am not managing to access the variable correctly.
How can I access the email from the .env file?

Instead of env("MAIL_USERNAME") try config('mail.username')
It first tries to get config variable value in the .env file and if it couldn't find the variable value in the .env file it will get variable value from config/mail.php

Related

Force laravel to send emial with a coustom configuration not used in .env file

I have a Laravel app, the code crypted by phpBolt (vondor folder not crypted) and I want to force to set an email configuration.
I try to changed from Mail class but it doesn't work
You can set your config on runtime using config()->set():
config()->set('mail.mailers.[your_mailer].[host/port/username/...]', 'value');

laravel modify how the password is checked in the authentication process

I am using laravel 5.2.
I would like to be able to change a parameter in the .env file like PASSWORD_VALIDATION=...
This could be either LARAVEL or PERSONAL.
If it is LARAVEL then it would use the standard Laravel authentication method with users and passwords in the db. But if I use PERSONAL, I would like that it uses a function I have created that will check if the email address is in the database then verify the password provided with the Active Directory in my company.
I looked at the various files and I can see I have:
app\Http\Controllers\Auth\AuthController.php
In there, I can see:
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
In this file:
vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
It uses
use AuthenticatesUsers, RegistersUsers {
AuthenticatesUsers::redirectPath insteadof RegistersUsers;
AuthenticatesUsers::getGuard insteadof RegistersUsers;
So I can see in the file
vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
My function to be changed which is:
public function postLogin(Request $request)
{
return $this->login($request);
}
I have tried to copy this one in my file app\Http\Controllers\Auth\AuthController.php but it doesn't change anything if I modify what is inside it...
Thanks
Ok, I found out why, it seems that in my routes, it is pointing to #login and not to #postLogin hence any modification applied to the function postLogin wouldn't do anything

Laravel not sending email from server and not giving errors

I have problem,
when I send mail from localhost everything works fine,
but when send from server I don't receive mail and I don't get error.
My env. file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xxxx#gmail.com
MAIL_PASSWORD=xxxx
and function
protected function contactMe() {
Mail::send('request2e', array(
'subject' =>Input::get("subject"),
'email' => Input::get("email"),
'message1' => Input::get("message1"),
'number' => Input::get("number")
), function ($message) {
$message->from('xxxx#gmail.com', 'Contact');
$message->to('yyyy#gmail.com')->subject('Contact');
});
return redirect('/');
}
Have any idea what could be the problem?
If you're using Gmail smtp you have to use the smtp driver:
MAIL_DRIVER=smtp
MAIL_PORT=587
MAIL_ENCRYPTION=tls
Make sure to clear the config cache if you have to (required in production).
php artisan config:cache
Everything else seems to be aok.
In your Gmail settings page do the following:
Click on the Forwarding/IMAP tab and scroll down to the IMAP Access section: IMAP must be enabled in order for emails to be properly copied to your sent folder.
I think you there is a problem with google app security functionality.
From above given link, turn on this functionality and check for this issue.
Hope this helps you.!

Change Mailgun to SMTP at Runtime in Laravel

Laravel version 5.2.22
I have tried these to change the mailgun to smtp at runtime:
config(['MAIL_DRIVER' => 'SMTP']); //not work.
Config::set('MAIL_DRIVER', 'SMTP'); //not work.
Finally, I get these work:
Config::set('mail.driver', 'SMTP'); //works.
config(['mail.driver'=>'smtp']); //works.
I don't really know why have to change the MAIL_DRIVER to mail.driver.
Config::get('mail.driver'); //you can use this to check the config.
The answer Alex gave himself is the solution, it should be:
Config::set('mail.driver', 'SMTP');
The following does not work, as usually the env variable defining the mail driver is called MAIL_DRIVER, not the config variable. (See config/mail.php for the assignment of config('mail.driver')).
Config::set('MAIL_DRIVER', 'SMTP'); //config var MAIL_DRIVER does not exist yet / is not used.

Laravel 5.2 reset password + Mandrill

I have Laravel 5.2 fresh installation.
I did following:
I have set up my .env file
MAIL_DRIVER=mandrill
SECRET=my_mandrill_api_key
I have installed Guzzle (https://github.com/guzzle/guzzle)
I have setup my email in view (https://github.com/laravel/laravel/blob/5.0/resources/views/emails/password.blade.php)
I have fixed the certificate issue (PHP cURL error code 60)
So it seems everything is done correctly.
When I fill email to reset password and press Send Password Reset Link button, I get following error
Server error: POST
https://mandrillapp.com/api/1.0/messages/send-raw.json resulted in a
500 Internal Server Error response:
{"status":"error","code":-1,"name":"ValidationError","message":"You
must specify a key value"}
I have check my log in Mandrill (https://mandrillapp.com/settings/api) there is no logs for my action.
From the error of Mandrill, I know it does not get the api key, so for some reason the api key and other values is not passing over to Mandrill api.
Question: What is missing/wrong?
Note: right now I am working on my local environment building the app. My local environment Windows 10/Bitnami WAMP stack 7/ I am also using Mandrill API.
If you are using the Mandrill driver then you must set the MANDRILL_SECRET in your .env file.
The other settings MAIL_HOST, MAIL_PORT and so on are for use with other drivers.
See the documentation here about using the Mandrill driver. It mentions that you should set the Mandrill key in config/services.php but you should really set that using an environment variable, so if you have customized the file change it back to
'mandrill' => [
'secret' => env('MANDRILL_SECRET'),
],
Then you will be able to read the MANDRILL_SECRET value from the .env file.
So in your .env file you'll have;
MAIL_DRIVER=mandrill
MANDRILL_SECRET=your_mandrill_key_here

Resources