Laravel 4 Email not working - laravel

I've been looking for the answer for two days and still nothing. I've followed several tutorials where nothing more than just a few config settings are required, but my Laravel 4 app still doesn't want to send an e-mail. It always throws the same Exception and nobody seems to have the answer. It seems that I've tried everything but the working one.
Route::get('/sendemail', function() {
$data['user'] = 'Test User';
Mail::send('email.test', $data, function($m) {
$m->to('myemail#example.com')->subject('Email test');
});
});
But when I'm in the specified route, it always throws the same error:
Argument 1 passed to Swift_Transport_EsmtpTransport::__construct()
must implement interface Swift_Transport_IoBuffer, none given
The config is ok and even changing the driver from 'smtp' to 'mail' in config/mail.php throws also Exception.
Argument 1 passed to Swift_Transport_MailTransport::__construct() must be an instance of Swift_Transport_MailInvoker, none given
I'm stuck in here and I don't know that to do. My composer is up to date and PHP version is 5.4.4.
Any advice will be much appreciated.
Thank you.

You have probably found an answer by now.
But I think you need a second argument for the to.
Try:
$data = array('user' => $user);
Mail::send('email.test', $data, function($message) use ($user)
{
$message->to($user->email, $user->name)->subject('Email Test');
});

Related

Laravel Dusk Sessions on

I have a laravel 5.4 project set up on a vagrant machine and I'm trying to create Laravel Dusk tests on it. It was a bit tricky and I followed the tutorial here : http://www.jesusamieiro.com/using-laravel-dusk-with-vagrant-homestead/
It runs fine, but the session variables don't seem to be saving. I have this code in a post route
if (!session('user_name')) {
$request->session()->flash("message", "Your session expired. Click "Please Confirm" to approve your monthly donation.");
return redirect()->back();
}
and it is set on the get request when the page is first fetched. I have this test in dusk:
$browser->visit("http://support.welcome.dev/?layout_id={$layout}&purl={$supporter->purl}&user_name={$supporter->name_first}&amount=10&ct={$hash}")
->press("Please Confirm")
->assertPathIs("/confirm");
This test fails and is redirected with the message session has expired. However, it works just fine when when the code in the post route checking for the user_name variable is commented out. The original code works when I test it manually on a browser.
Any help is appreciated.
$user = factory(User::class)->create([
'email' => 'taylor#laravel.com',
]);
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->type('email', $user->email)
->type('password', 'secret')
->press('Login')
->assertPathIs('/home');
});
This code is write on https://laravel.com/docs/5.4/dusk. i think this can help you. =)

Laravel 5 and Socialite - New Redirect After Login

Another newb question here, but hopefully someone can shed some light:
I am using Socialite with Laravel 5, and I want to be able to redirect the user to a page on the site after they have logged in. The problem is that using
return redirect('any-path-I-put-here');
simply redirects back to 'social-site/login?code=afkjadfkjdslkfjdlkfj...' (where 'social-site' is whatever site is being used i.e. facebook, twitter, google, etc.)
So, what appears to me to be happening is that the redirect() function in the Socialite/Contracts/Provider interface is overriding any redirect that I attempt after the fact.
Just for clarification, my routes are set up properly. I have tried every version of 'redirect' you can imagine ('to', 'back', 'intended', Redirect::, etc.), and the method is being called from my Auth Controller (though I have tried it elsewhere as well).
The question is, how do I override that redirect() once I am done storing and logging in the user with socialite? Any help is appreciated! Thank you in advance.
The code that contains the redirect in question is:
public function socialRedirect( $route, $status, $greeting, $user )
{
$this->auth->login( $user, true );
if( $status == 'new_user' ) {
// This is a new member. Make sure they see the welcome modal on redirect
\Session::flash( 'new_registration', true );
return redirect()->to( $route );// This is just the most recent attempt. It originated with return redirect($route);, and has been attempted every other way you can imagine as well (as mentioned above). Hardcoding (i.e., 'home') returns the exact same result. The socialite redirect always overrides anything that is put here.
}
else {
return redirect()->to( $route )->with( [ 'greeting' => $greeting ] );
}
}
... The SocialAuth class that runs before this, however, is about 500 lines long, as it has to determine if the user exists, register new users if necessary, show forms for different scenarios, etc. Meanwhile, here is the function that sends the information through from the Social Auth class:
private function socialLogin( $socialUser, $goto, $provider, $status, $controller )
{
if( is_null( $goto ) ) {
$goto = 'backlot/' . $socialUser->profile->custom_url;
}
if( $status == 'new_user' ) {
return $controller->socialRedirect($goto, $status, null, $socialUser);
}
else {
// This is an existing member. Show them the welcome back status message.
$message = 'You have successfully logged in with your ' .
ucfirst( $provider ) . ' credentials.';
$greeting =
flash()->success( 'Welcome back, ' . $socialUser->username . '. ' . $message );
return $controller->socialRedirect($goto, $status, $greeting, $socialUser);
}
}
I managed to workaround this problem, but I am unsure if this is the best way to fix it. Similar to what is stated in question, I got authenticated callback from the social media, but I was unable to redirect current response to another url.
Based on the callback request params, I was able to create and authenticate the user within my Laravel app. It worked good so far but the problems occured after this step when I tried to do a return redirect()->route('dashboard');. I tried all the flavours of redirect() helper and Redirect facade but nothing helped.
The blank page just stared at my face for over 2 days, before I checked this question. The behaviour was very similar. I got redirect from social-media to my app but could not further redirect in the same response cycle.
At this moment (when the callback was recieved by the app and user was authenticated), if I refreshed the page manually (F5), I got redirected to the intended page. My interpretation is similar to what's stated in this question earlier. The redirect from social-media callback was dominating the redirects I was triggering in my controller (May be redirect within Laravel app got suppressed because the redirect from social-media was still not complete). It's just my interpretation. Experts can throw more light if they think otherwise or have a better explaination.
To fix this I issued a raw http redirect using header("Location /dashboard"); and applied auth middleware to this route. This way I could mock the refresh functionality ,redirect to dashboard (or intended url) and check for authentication in my DashboardController.
Once again, this is not a perfect solution and I am investigating the actual root of the problem, but this might help you to move ahead if you are facing similar problem.
I believe you are overthinking this. Using Socialite is pretty straight forward:
Set up config/services.php. For facebook I have this:
'facebook' => [
'client_id' => 'your_fb_id',
'client_secret' => 'your_fb_secret',
'redirect' => '>ABSOLUTE< url to redirect after login', //like: 'http://stuff'
],
Then set up two routes, one for login and one for callback (after login).
In the login controller method:
return \Socialize::with('facebook')->redirect();
Then in the callback function
$fb_user = \Socialize::with('facebook')->user();
// check if user exists, create it and whatnot
//dd($fb_user);
return redirect()->route('some.route');
It should be pretty much similar for all other providers.
We are using the Socialite login in our UserController as a trait. We simply overrode the AuthenticatesSocialiteLogin::loginSuccess() in our controller.
use Broco\SocialiteLogin\Auth\AuthenticatesSocialiteLogin;
class UserController extends BaseController
{
use AuthenticatesSocialiteLogin;
public function loginSuccess($user)
{
return redirect()->intended(url('/#login-success'));
}
....

Whoops & Laravel 4.1.26

I recently upgraded to 4.1.26 on my local machine and now it seems that whoops isnt working correctly. All I am getting at the moment is a blank screen with a title as seen below, can anyone tell me why this might be happening?
http://imageshack.com/a/img841/820/tpcm.png
I can't tell what's wrong with your application until you at least enable debugging.
As a first guess though, it might be the new remember_token that was introduced in 4.1.26. A lot of people are having issues adjusting to it.
Have you updated your User model yet? If not, open:
app/models/User.php
and add the following methods:
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
Then, add a new column called remember_token (of type VARCHAR(100), TEXT, or whatever equivalent you want) to your users table. It must be nullable.
Finally, when you're ready to authenticate a user, just add true as the second argument to attempt method.
if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
// At this point the user is remembered
}
That's it. Hope this solves your issue. Happy coding!
Source: Laravel Upgrade Guide

Laravel syntax error in variable while nothing changed

I am making a mobile app with a username-password login screen. Yesterday i left my computer on when i was done with work. Today i sit behind my computer and try to log in. Strange thing is i get this error:
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"syntax error, unexpected '$credentials' (T_VARIABLE)","file":"C:\\Google Drive\\htdocs\\laravel4_test4\\app\\controllers\\MobileController.php","line":18}}
Weirdest thing of all is i haven't chaged any code since yesterday. This is the part of my controller code that seems to give the syntax error.
public function login(){
$input = Input::all()
$credentials = array('username' => $input['name'], 'password' => $input['password']);
if(Auth::attempt($credentials)){
// HERE COMES THE REST
There seems to be nothing wrong with the code. Does anyone know why this could be happening?
You missed a semicolon after $input = Input::all()
$input = Input::all();

Laravel 4 exception handling - same code works in 1 case and not in 2 other cases

I am trying to handle a few exceptions in Laravel 4, and the same snippet of code works for one of them but not for the others.
I declared an Exceptions.php file that I added to my composer.json.
In Exceptions.php, I declared the following:
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Class NotAllowedException extends Exception {};
The following handler, in Exceptions.php, works properly:
// snippet 1
App::error(function(NotAllowedException $e, $code, $needed)
{
return Response::make(View::make('special.error')->with('error', array('headline' => 'Not Allowed',
'description' =>'<p>Your user has not enough privileges to perform the requested operation.</p>')), 401);
});
When I try to do the same to handle HttpNotFoundExceptions and ModelNotFoundExceptions, the following 2 snippets which are copies of the first do not work and yield an Undefined variable: error error.
// snippet 2
App::error(function(ModelNotFoundException $e)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});
// snippet 3
App::error(function(NotFoundHttpException $e)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});
I was only able to make the NotFoundHttpException work by putting this into global.php:
App::missing(function($exception)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});
But I do not know why it works if I put it there and it does not work if I put it in Exceptions.php.
Trying to put snippets 2 and 3 in global.php yield in both case an Internal Server error.
To recap, Questions:
I think I am following the ModelNotFoundException handling as per Laravel4's documentation; what What am I doing wrong / how to make it work?
why does the App::missing work only if I put it in global.php ? I fear that I might be missing something important about Laravel's inner workings.
Thanks in advance to anyone who can shed some light on those 2 issues

Resources