Sending email in laravel 5 not working - windows

After long searches in the forum, I can't find a helpful solution for my case.
I did alot of functions for sending emails in laravel and always that works fine.
This time, I got an error as you see below:
ErrorException in StreamBuffer.php line 95:
stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Here my .env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:FMtA/k/okcPZB/HbWGbw5YiBM4EC3njxxLbgcdM1GrA=
APP_URL=http://localhost
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=myDB
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=suckerblood2010#gmail.com
MAIL_PASSWORD=<<Here_my_password>>
MAIL_ENCRYPTION=tls
MAIL_SENDER=Administration
Here my function to send the mail (in the controller Auth):
public function register(Request $request)
{
$this->validateLogin($request);
$token = hash('sha256', str_random(100) . time(), false);
$user = new User();
$user->email = $request->get('email');
$user->password = bcrypt($request->get('password'));
$user->remember_token = $token;
//$user->save();
$sent = Mail::send('auth.emails.createAccount', ['token' => $token], function ($m) use ($user) {
$m->from(getenv('MAIL_USERNAME'), getenv('MAIL_SENDER'));
$m->to($user->email, $user->email)->subject(config('constants.AccountCreated'));
});
if($sent == 1)
{
$msg = Lang::get('messages.EmailSent');
$this->showLoginForm($msg);
}
else
{
return redirect('/login')->withErrors([
'email' => Lang::get('messages.UserAddingError')
]);
}
}
I tried to change the protocol from tls to ssl, even the prot from 587 to 465
I have this problem only with this project and I can't understand why, despite all the searches I made...
Any suggestion please?
Thanks alot :)

Your root certificate is missing or corrupted, try installing one
# 1) Download a valid root ca
http://curl.haxx.se/ca/cacert.pem
# 2) Setup php.ini to point it
openssl.cafile=C:\php5\etc\cacert.pem
To make sure they are loaded, review this console command response
php -r 'var_dump(openssl_get_cert_locations());
You may need to restart your webserver
You can also disable ssl peer verification in php.ini, but it is not recommend (please don't do this in production!)
openssl.verify_peer 0
Useful docs: http://php.net/manual/migration56.openssl.php

Related

Laravel Lumen function env() returns null sometimes

I am developing api with Lumen 6.2.0 which gets GET request with certain parameters and token. When it gets parameters it process it in a certain way and then encode with a secret key which is in my .env file and then compares result with the token which was provided with the request, if comparison result is true then user is authenticated else he is not. So the problem is sometimes env() function returns null. It doesn't happen pretty often, just like 1 request out of 15, but it's still a serious problem for me.
I googled a lot but found just few approaches. Firstly I found out that env() function should be only invoked in config file and since Lumen doesn't have a config directory and config files I have created it, but the issue remains the same. The second advice was for Laravel specifically - php artisan config:clear and php artisan config:cache but Lumen doesn't have such commands, although I ran the php artisan cache:clear command to no avail. So here is my code:
.env file
APP_NAME=Example
APP_ENV=local
APP_KEY=ApPkEyHeRe
APP_DEBUG=true
APP_URL=https://example.com
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD=dbpass
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
VK_APP_SECRET=SoMeFaNcYkEy
config/config.php
<?php
return [
'vk_app_secret' => env('VK_APP_SECRET'),
'events_per_page' => 16
];
And UsersController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class UsersController extends Controller
{
public function check(Request $request) {
$query_params = $request->all();
$sign_params = [];
foreach ($query_params as $name => $value) {
if (strpos($name, 'vk_') !== 0) {
continue;
}
$sign_params[$name] = $value;
}
ksort($sign_params);
$sign_params_query = http_build_query($sign_params);
$secret = config('config.vk_app_secret');
$hash_hmac = hash_hmac('sha256', $sign_params_query, $secret, true);
$base_encode = base64_encode($hash_hmac);
$trim_chars = strtr($base_encode, '+/', '-_');
$sign = rtrim($trim_chars, '=');
$status = $sign === $query_params['sign'];
return json_encode($status);
}
}
I also logged every line of this algorithm, and noticed an interesting thing, the failing case contains [date] production.INFO: prefix before log's rows, and every successful case [date] local.INFO: So maybe it's affecting env() function somehow? I also don't get it why it sometimes logged as production when I have APP_ENV=local

I am getting error when i am sending mail in laravel

I am getting error when i am sending mail at gmail id,I am using laravel 5.7,
I am getting this errot
Call to undefined function App\Http\Controllers\send()
and my controller code is :
public function postEmail(Request $r)
{
$data=[
'user_email'=>$r->user_email,
'user_name'=>$r->user_name,
'user_phone'=>$r->user_phone,
'user_desc'=>$r->user_desc,
];
Mail:send('mail.mail', $data, function($user_desc) use($data)
{
$message->from('sumitsaoni#gmail.com', 'Send by Sumit');
$messge->to($data['sumit123#gmail.com']);
$message->subject($data['user_desc']);
});
return redirect()->back()->with('message','successfully data insertd');
}
And My Route is
Route::resource('contact', 'ContactController');
Route::post('/postEmail', 'ContactController#postEmail');
My .env file mail gmail: .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=my_email (Using my email id)
MAIL_PASSWORD=password (using my email id password)
MAIL_ENCRYPTION=tls
You should try this:
use Mail;
Mail::send('mail', $data, function($user_desc) use($data)
{
$message->from('sumitsaoni#gmail.com', 'Send by Sumit');
$message->to($data['user_email']);
$message->subject($data['user_desc']);
});
Updated answer
Mail::send('mail', $data, function($message) use($data)
{
$message->from('sumitsaoni#gmail.com', 'Send by Sumit');
$message->to($data['user_email']);
$message->subject($data['user_desc']);
});
change
Mail:send('mail.mail', $data, function($user_desc) use($data)
to
Mail::send('mail.mail', $data, function($user_desc) use($data)
try this one
Mail:send to replace Mail::send

Environment stays Local when deploying Laravel to Heroku

I'm trying to set dev + production environment in Laravel 5, but when i check the current environment - i get Local in both cases...when i work local and when i deploy to Heroku...
I want to create the 2 environments for switch from https to http smoothly...
Here is the AppServiceProvider.php file...
<?php
namespace App\Providers;
use App;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$environment = App::environment();
echo '<pre>';
print_r($environment); // prints Local - always
echo '</pre>';
die();
if (App::environment('production', 'staging'))
{
\URL::forceSchema('https');
}
else
{
echo "environment=", App::environment(), "\n";
}
}
public function register()
{
//
}
}
And here is the .env file
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:foo
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=foo
DB_PORT=3306
DB_DATABASE=foo
DB_USERNAME=foo
DB_PASSWORD=foo
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
You need to change your .env file on your Heroku server to reflect that it's production. You should keep your APP_ENV=local on your local development machines, but change it to APP_ENV=production on your live server and APP_ENV=staging on your staging server.
Edit:
In the root of your local server, you must have a .env file that has APP_ENV=local in it (the rest can stay as above in your question). On your production server, you must have the same .env file, but change APP_ENV=local to APP_ENV=production and also edit the database connection data if required. You don't need any additional files/folders, .env files are environment specific.
And then this code will work fine (look at my comments)
$environment = App::environment();
echo '<pre>';
print_r($environment); // prints Local - always - but this won't when you have the 2 differing environment files
echo '</pre>';
die();
if (App::environment('production', 'staging'))
{
\URL::forceSchema('https');
}
else
{
echo "environment=", App::environment(), "\n";
}
Because you have separate .env files on each server. They are not meant to be committed inside a repository, they must be different and in the root of the project.

Laravel forgot password emails not being sent

I have an extremely odd issue with Laravel emails.
Generally speaking I can send emails fine like so:
Mail::send('foo', $templateData,
function (Message $message) use ($name, $email) {
$message
->from('admin#example.com', 'My Example Company')
->to($email, $name)
->subject('Blah');
}
);
However, when trying to reset password, an email is not sent on my production server, despite acting like it has sent (i.e. no exceptions!). It is however sent when using mailtrap.io.
This is how my .env file looks like:
APP_ENV=prod
APP_KEY=XXXX
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://example.co.uk
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=XXX
DB_USERNAME=XXX
DB_PASSWORD=XXX
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=XXX
MAIL_PORT=XXX
MAIL_USERNAME=XXX
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=ssl
MAIL_FROM=admin#example.com
MAIL_FROM_NAME="My Example Company"
GOOGLE_API_KEY=XXX
I also tried:
composer dump-autoload
php artisan clear-compiled
php artisan config:clear
But to no avail.
Interestingly, when I use Mailer as opposed to the Mail facade, the email is again not sent with no exceptions raised.
public function foo(Illuminate\Mail\Mailer $mailer)
{
$mailer->send('ViewName', [],
function (Message $message) {
$message
->from('admin#example.com', 'My Example Company')
->to('you#example.com', 'John Smith')
->subject('Blah');
}
);
}

RequestException in CurlFactory.php line 187

I'm using Laravel 5.1 and want to send email but I'm getting this error:
RequestException in CurlFactory.php line 187:
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
How could I solve this?
My .env file:
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=oEontENjBIdIrzaXhk9v9Q
My function in controller:
public function mailContact(ContactRequest $request)
{
$name = $request->input('name');
$email = $request->input('email');
$body = $request->input('body');
$sent = Mail::send('emails.contact', compact('name', 'email', 'body'), function ($message) {
$message->to('khudadadrs#gmail.com', 'Admin')->subject('Message');
});
if ($sent) {
return Redirect::back()->withMessage('تشکر از اینکه تماس گرفتید.');
}
return Redirect::back()->withError('متاسفانه ایمیل ارسال نشد.دوباره تلاش کنید');
}
Download the https://gist.github.com/VersatilityWerks/5719158/download
Extract it an place it in ..\php\ext (curl.cainfo="E:\xampp\php\ext)"
Open the php.ini and write after the php_curl.dll: curl.cainfo="..\php\ext\cacert.pem"(curl.cainfo="E:\xampp\php\ext\cacert.pem")
Restart the web server.
I added the cacert.pem file as described in http://codeontrack.com/solve-laravelxamppguzzlehttp-curl-error-60-no-sll-certificate/
but now I'm getting this error:
RequestException in CurlFactory.php line 187:
cURL error 77: error setting certificate verify locations:
CAfile: [pathtothisfile]\cacert.pem
CApath: none (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Any help?

Resources