the password recovery email in laravel is not sent to the user
and this is my function in controller :
public function recover(Request $request)
{
$validator = Validator::make($request->only('email'), [
'email' => 'required'
]);
if($validator->fails()) {
throw new ValidationHttpException($validator->errors()->all());
}
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject(Config::get('boilerplate.recovery_email_subject'));
});
switch ($response) {
case Password::RESET_LINK_SENT:
// return $this->response->noContent();
return response()->json(['success' => true, 'data' => $response], 200);
case Password::INVALID_USER:
// return $this->response->errorNotFound();
return response()->json(['success' => false, 'data' => $response], 200);
}
}
and I configure my .env and mail.php
I use laravel 5.6
if you are using gmail address to sent mail. then you need to on less secure app.
go to https://myaccount.google.com/lesssecureapps
then allow by turning it on.
and also use
php artisan config:clear
sometimes google blocks when you try to send email through some code. In that case you got a alert mail. click on that mail(Check Activity) and mark as yes (Do you recognize this activity?)
Or you can try
MAIL_DRIVER=sendmail
and also use
php artisan config:clear
First of all, try to locate the problem.
I would suggest to try to setup MAIL_DRIVER=log and check laravel.log after executing this function. If nothing is found in the log - then you're not trying to send it. Most common problem, in this case, is using queues, so, check the QUEUE_CONNECTIONvariable in .env, it should be equal to "sync" (or set up your driver, like Redis if required).
By using "log" driver you should see your message in the log. If it works fine with "log" and doesn't work with smtp then you should get an error about it. If an error is present - please post it. If not - say so as well.
P.S. please note that if you're using php artisan serve, all variables from .env are updates only after restarting the php server.
Related
For each user, I change the email configuration in laravel project, when user login i use this:
\Config::set(['mail.mailers.smtp.host' => $user->smtp_mail_host]);
\Config::set(['mail.mailers.smtp.port' => $user->smtp_mail_port]);
\Config::set(['mail.mailers.smtp.username' => $user->smtp_mail_username]);
\Config::set(['mail.mailers.smtp.password' => $user->smtp_mail_password]);
\Config::set(['mail.from.address' => $user->mail_address]);
\Config::set(['mail.from.name' => $user->mail_from_name]);
when I did config() it is showing my change, but when I send, the mail is sent from the .env configuration mail address, there is a missing part I'm not finding it.
thanks for help
This is not optimal solution but can help! Pass the Configuration to your Mail::send before call send method such like this:
First import these classes in your Controller
use Mail;
use \Swift_Mailer;
use \Swift_SmtpTransport as SmtpTransport;
Then in your send function:
$from_email = \Config::get('mail.from.address');
$email_name = \Config::get('mail.from.name');
$to_email = "test#test.com";
$transport = (new SmtpTransport(\Config::get('mail.mailers.smtp.host'), \Config::get('mail.mailers.smtp.port'), null))->setUsername(\Config::get('mail.mailers.smtp.username'))->setPassword(\Config::get('mail.mailers.smtp.password'));
$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
Mail::send('emails.emailhtmlpage', [], function ($m) use ($from_email,$email_name,$to_email) {
$m->from($from_email, $email_name);
$m->to($to_email, 'TEST')->subject("Your Email Subject");
});
The settings applied using Config::set do not persist across different requests: if you only run them when the user logins, the changes with not apply to the following requests.
You should modify your Authenticate middleware adding:
public function handle($request, Closure $next, ...$guards)
{
$authReturned = parent::handle($request, $next, $guards);
$user = Auth::user();
\Config::set(['mail.mailers.smtp.host' => $user->smtp_mail_host]);
\Config::set(['mail.mailers.smtp.port' => $user->smtp_mail_port]);
\Config::set(['mail.mailers.smtp.username' => $user->smtp_mail_username]);
\Config::set(['mail.mailers.smtp.password' => $user->smtp_mail_password]);
\Config::set(['mail.from.address' => $user->mail_address]);
\Config::set(['mail.from.name' => $user->mail_from_name]);
return $authReturned;
}
if you using Queue and Jobs make sure to
stop the php artisan queue:work first (its seems to have it own cache where it store the configs)
check background process to make sure queue:work already stop (for linux you can use top -c -p $(pgrep -d',' -f php) and search for php artisan queue:work in COMMAND column )
do the change you want in .env or configs
config:clear
redo php artisan queue:work
I am trying to build a custom configuration that gets added to my Laravel configuration at boot up. I created an artisan command that allows the developers to place the server into 'test' mode and 'live' mode. This should then force API's to use test keys rather than live keys which works fine.
public function handle()
{
switch(strtoupper($this->argument('m'))) {
case 'LIVE':
\DB::table('server_mode')->where('id', 1)->update([
'mode' => true,
'verified_by' => 'Server'
]);
$this->info('Server is now in LIVE mode');
break;
case 'TEST':
\DB::table('server_mode')->where('id', 1)->update([
'mode' => false,
'verified_by' => 'Server'
]);
$this->info('Server is now in TEST mode');
break;
default:
$this->error('Please enter either LIVE or TEST');
break;
}
}
The problem is, I want to add which developer changed the status. So far, it is hard coded to 'Server'. Is it possible to get the IP or PC information who'm executed the command to store in the DB?
As said in the comments, Jaquarh doesn't try to authenticate. He just wants to get the username of the perso who ran the command.
You can use $processUser = exec('whoami'); to get the current user name. (It won't work on windows)
However, be careful, usernames are not always very informative. My laptop at work came preinstalled with a user name "User".
Another idea that I can suggest is to force a "name" parameter when using your command.
I am using Laravel 5.1
I created a function to get smtp info from db for a user $mail_config=STMPDetails::where('user_id',36)->first() and then I can just call config helper function and pass the array to set config value config($mail_config). and then I call Mail::queue function.
but before it reaches createSmtpDriver#vendor/laravel/framework/src/Illuminate/Mail/TransportManager.php where it reads the configuration again to send the mail, mail config are changed to the one specified in .env file.
Another thing to note is Mail sending function is in a Listener
I am not able to figure out where can I call the function such that configuration changes are retained before mail is sent.
Thanks,
K
This should work:
// Set your new config
Config::set('mail.driver', $driver);
Config::set('mail.from', ['address' => $address, 'name' => $name]);
// Re execute the MailServiceProvider that should use your new config
(new Illuminate\Mail\MailServiceProvider(app()))->register();
Since the default MailServiceProvider is a deferred provider, you should be able to change config details before the service is actually created.
Could you show the contents of $mail_config? I'm guessing that's the problem. It should be something like
config(['mail.port' => 587]);
Update - tested in 5.1 app:
Mail::queue('emails.hello', $data, function ($mail) use ($address) {
$mail->to($address);
});
->> Sent normally to recipient.
config(['mail.driver' => 'log']);
Mail::queue('emails.hello', $data, function ($mail) use ($address) {
$mail->to($address);
});
->> Not sent; message logged.
I've tried to using queue for everytime user register and send an email to them to verify.
I'm doing it successfully using Laravel 5.1
I just wandering how can I can stop current queue if I got an error and then when I fix it I restart the job from the last queue?.
How about the error like this:
[InvalidArgumentException]
View [emails.versify_email] not found.
[InvalidArgumentException]
View [emails.versify_email] not found.
[InvalidArgumentException]
View [emails.versify_email] not found.
I've tried at homestead using:
public function failed(){
//I've tried send email but it not sending
}
or at AppServiceProvider
Queue::failing(function ($connection, $job, $data) {
$user ='mymail#gmail.com';
Mail::send('emails.fail_queue', ['user' => $user], function ($m) use ($user) {
$m->subject('Failing:' . $user)
->to($user);
});
});
None of the them is working.
what should i do if its like that when happened at production?.
When an exception is thrown from the handler Laravel tries to release the job back to the queue unless it is explicitly deleted.
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Queue/Worker.php#L198-L227
You can use the $delay parameter to put back in the queue with delay. Or better just bury the job yourself, if you are able to detect the issue.
$this->job->bury()
I am having trouble setting up my delayed mailing with laravel 5.1
In App\Exceptions\Handler in the render method I want to mail any occuring errors to myself: Like this
Mail::send('emails.servererror', ['exception' => $e, 'request' => $request] ,function($message)
{
...
});
This works perfectly, except that I would like to delay the sending of the email With Mail::later. E.G.
Mail::later(5, 'emails.servererror', ['exception' => $e, 'request' => $request] ,function($message)
{
...
});
The part where I am having trouble is the following:
This method will automatically take care of pushing a job onto the queue to send the mail message in the background. Of course, you will need to configure your queues before using this feature.
I have read the documentation several times. I still can't figure out what precisely I should do to make the Mail::later work.