Laravel 5.1 Mail::send doesn't work - gives 500 internal server error - laravel

I took an example from the Laravel 5.1 documentation for Mail and replaced it with my send and receiver email ids.
Mail::raw works in the controller and if I use Mail::send in tinker it works. However, if I use Mail::send in the controller it doesn't work.
Everything is set up as described on the Laravel 5.1 mail page. https://laravel.com/docs/5.1/mail#sending-mail . I have also cleared app cache, config cache and view cache.
public function sendEmailReminder(Request $request, $id)
{
$user = User::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
}

The error could be from the email you are trying to send from, in order to send the email successfully this email hello#app.com has to be a valid email address and the one register as your MAIL_USERNAME

It was a permissions issue for the storage/frameworks directory. Once I changed the permissions it worked fine..

Related

How can write test for laravel api route with auth.basic middleware

My laravel project has an API route by auth.basic middleware which is used id of the authenticated user in the controller. when I call it in postman it works well and I get 401 when the username or password is incorrect, but in laravel APITest which extends from DuskTestCase, authentication does not take place so I get 500 instead of 401 when the user's informations were incorrect. However, by correct information, I have the same error because auth() is null.
it is written like below, which is wrong?
api.php route:
Route::get('/xxxxxx/xxxxxx/xxxxxxx', 'xxxxx#xxxx')->middleware('auth.basic');
APITest:
$response = $this->withHeaders(['Authorization' => 'Basic '. base64_encode("{$username}:{$password}")])->get("/xxxxxx/xxxxxx/xxxxxxx");
You can use actingAs() method for authentication in tests.
An example from docs:
public function testApplication()
{
$user = factory(App\User::class)->create();
$this->actingAs($user)
->withSession(['foo' => 'bar'])
->visit('/')
->see('Hello, '.$user->name);
}
Another example that you can use for an API:
$user = User::factory()->create();
$response = $this->actingAs($user)->json('GET', $this->uri);
$response->assertOk();
For more information: https://laravel.com/docs/5.1/testing#sessions-and-authentication

Auth is not working inside the Stripe Webhook Function in Stripe [Laravel]

I am trying to access Authenticated User information inside the Stripe-Webhook-Url function. But when webhook url hits, the Auth inside that function doesn't return anything!
Here is the route and action I am working with:
route
Route::post('stripe-webhook', [StripeController::class, 'stripe_webhook']);
action
public function stripe_webhook(Request $request)
{
$response = $request->all();
$user_id = Auth::id(); // It returns Nothing.....
}
In the above function, The Auth returns nothing. Even session doesn't work inside this function.
I am trying to debug this issue but nothing worked out. Kindly help me to sort out this problem.
Thanks!

Login by code seems to not work in laravel

Basically i'm trying to send by email a link that lets you login with a specific account and then redirects you to a page.
I can seccessfully generate link and send them via email using URL functionalities in laravel using this code:
Generating the link:
$url = "some/page/".$travel_id;
$link = URL::temporarySignedRoute(
'autologin', now()->addDay(), [
'user_id' => 3,
'url_redirect' => $url,
]
);
And sending the mail:
Mail::send('emails.travel', $data, function ($message) use ($data) {
$message->from('mail#mail.com', 'blablabla');
$message->to('reciever#mail.com', 'blablabla')->subject('test');
});
There is a route that catches the link sent by mail that is supposed to log you in with the user (in this case, the one with the id '3') and redirect you to some page but when it redirects, it prompts you to the login page, as if you are not logged.
Here is the route:
Route::get('/autologin', function (Request $request) {
$user = User::findOrFail($request->user_id);
if (! $request->hasValidSignature()) {
abort(403);
}
Auth::login($user);
return redirect($request->input('url_redirect'));
})->name('autologin');
When i try to do a Auth::check() after the Auth::login($user); it returns true, so is the user logged in?
I also tried to use Auth::loginUsingId($request->user_id); with no different results.
Any idea of what's happening?
So i found the problem,
I was logging in with a backpack user but i was using the default auth feature of laravel.
Turns out i need to use: backpack_auth()->login($user); instead of Auth::login($user); if i want to login using a backpack user.
Also use backpack_auth()->check() instead of Auth::check().

Sending a registration request to Laravel

I'm trying to send a post request to my Laravel app so that I could create a User without the UI.
I've tried sending a post request via cURL:
curl --data "name=test&password=password120918&email=app#test.com" http://localhost:8080/register
This didn't work.
This is a fresh install of Laravel 5.4
I can't find anything to do with the RegisterController in the routes/web.php file.
What would the url to register a user be for Laravel 5.4? (I'm pretty sure it works the same way as 5.3)
Thank you.
I know that in laravel there is a default way to do things, But if your just looking to create a user from post request and send it back as a response you can do it yourself.
in your routes.php
Route::post("/users", "UsersController#store");
Then create a UsersController.php and add the method:
public function store(Request $request){
//You should add validation before creating the user.
$user = App\User::create([
"email" => $request->email,
"name" => $request->name,
"password" => bcrypt($request->password)
]);
if(!$user){
return response(["error" => "Your error here"], 400);
}
return response(["user" => $user], 200);
}
Then try it our with postman or curl command like
curl -X POST -F 'name=Testing User' -F 'password=pass1234' -F 'email=testing#gmail.com' http://localhost:8080/users
php artisan route:list
Will show you all of the registered routes for your application. When using Laravel's built in auth, routes are registered without them actually being in your routes file.
By default Laravel adds a POST route for /register to
App\Http\Controllers\Auth\RegisterController#register

Undefined variable inside Laravel view when using Mail

While trying to send verification email using Laravel 5.2, I get an error:
Undefined variable: confirmation_code (View:
C:\xampp\htdocs\laravel\resources\views\email\verify.blade.php)
My code looks like this:
Controller.php:
public function postSignup(Request $request){
$this->validate($request,[
'email'=>'required|unique:users|email',
'name'=>'required|max:50|min:3',
'password'=>'required|min:6',
'con-password'=>'required|same:password',
]);
$confirmation_code=['code'=>str_random(20)];
$name = $request->input('name');
Mail::send('email.verify',$confirmation_code,function($message)
use($request,$name){
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
User::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password'))
]);
return redirect()->back()->with('info','Congratulation you have been successfully registered.Please check your email for verification');
}
Mail.verify.blade.php:
<h2>Verify Your Email Address</h2>
<div>
Thanks for creating an account with the verification demo app.
Please follow the link below to verify your email address
{{ URL::to('register/verify/'.$confirmation_code) }}.<br/>
</div>
</body>
Try this:
Mail::send('email.verify', compact('confirmation_code'), function ($message) use($request, $name) {
$message->to($request->input('email'),$name)
->subject('Verify Your Email Address');
});
The reason why it fails is that Laravel views accept an associative array as their data, so that it can turn them into variables using keys as variables names and match them to their corresponding values.
What compact does is turn your variable into an associative array, with the name of the variable as its key (sort of the opposite of what the Laravel view will do).

Resources