I have problems with mail send in L5.1 Page with the form showing using GET method, but when I fill the form and click SEND button I see white screen with the only one word - Forbidden.
here is my code
app/Http/routes.php
Route::get('contacts', 'PageController@contacts');
Route::post('contacts', ['as' => 'sendmail', 'uses' => 'PageController@sendmail']);
app/Http/Controllers/PageController.php
<?php namespace App\Http\Controllers;
use App\Category;
use Mail;
use App\User;
use Illuminate\Http\Request;
use Form;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContactFormRequest;
class PageController extends Controller {
public function __construct()
{
}
public function contacts()
{
$category = Category::where('sef', '=', 'contacts')->first();
return view('pages.contacts')->withCategory($category);
}
public function sendmail(ContactFormRequest $request)
{
$category = Category::where('sef', '=', 'contacts')->first();
Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('admin@mail.ru');
$message->to('user@mail.ru', 'Admin');
$message->subject('Feedback');
});
return Redirect::route('contacts')->withCategory($category)
->with('message', 'Thanks for contacting us!');
}
app/Http/Requests/ContactFormRequest.php
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class ContactFormRequest extends Request {
public function authorize()
{ return false;
}
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
];
}
}
resources/views/pages/contacts.blade.php
...
<h2>Feedback form</h2>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
{!! Form::open(array('route' => 'sendmail', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Your Name') !!}
{!! Form::text('name', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your name')) !!}
</div>
<div class="form-group">
{!! Form::label('Your E-mail Address') !!}
{!! Form::text('email', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your e-mail address')) !!}
</div>
<div class="form-group">
{!! Form::label('Your Message') !!}
{!! Form::textarea('message', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your message')) !!}
</div>
<div class="form-group">
{!! Form::submit('Contact Us!',
array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
...
Mail configuration works well. I checked it by simply refreshing testpage with Mail::queue function inside. And every time I got message in my mail box.
like that
Route::get('testmail', 'PageController@testmail');
public function testmail()
{
$category = Category::where('sef', '=', 'contacts')->first();
Mail::queue('emails.contacts', array('name' => 'schel4ok'), function($message)
{
$message->subject('Feedback from site');
$message->from('admin@mail.ru', 'name' );
$message->to('user@smail.ru', 'user');
$message->getSwiftMessage();
});
return view('pages.testmail')->withCategory($category);
}
public function authorize()
{
return false;
}
You are returning false, which forbids access to everybody. You should change that to return true;
to allow everybody to submit a form.
Of course. I forgot this. Thank you very much. I also forgot to add use Redirect; to PageController.
But now I have error
InvalidArgumentException in UrlGenerator.php line 278:
Route [contacts] not defined.
Np. :)
The new issue is because the route()
method is for named routes. The to()
method is for a specific URI. In your routes file, you defined the contacts URI, but you didn't give it a "name". You only specified the uri and action.
// uri // controller action
Route::get('contacts', 'PageController@contacts');
Changing it like this assigned a "name" to your route.
// uri // route name // controller action
Route::get('contacts', ['as' => 'contacts', 'uses' => PageController@contacts']);
You can read more about that in the docs: http://laravel.com/docs/master/routing
Another option you can do is change the route()
method to the to()
method.
return Redirect::to('contacts')->withCategory($category)
->with('message', 'Thanks for contacting us!');
I want to use input information in $message variable like this
$message->from($request->input('email'), $request->input('name') );
or
$message->from($request->get('email'), $request->get('name') );
but have
ErrorException in PageController.php line 46:
Undefined variable: request
for me it is not obvious why inside one function $request->get('name') works in Array and then doesn't work in $message
public function sendmail(ContactFormRequest $request)
{
$category = Category::where('sef', '=', 'contacts')->first();
Mail::send('emails.contacts',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
),
function($message)
{
$message->from('admin@mail.ru', $request->get('name') );
$message->to('ipopov@mail.ru', 'Admin');
$message->subject('Письмо с сайта - страница контактов.');
});
return Redirect::route('contacts')->withCategory($category)
->with('message', 'Thanks for contacting us!');
}
From the PHP site themselves (http://php.net/manual/en/functions.anonymous.php):
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
So, try changing
function($message)
to
function($message) use ($request)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community