Create simple contact form in Laravel 4



1. Go to
app/config/mail.php
Change ‘drive’ from ‘smtp’ to ‘mail’
'driver' => 'mail',
Next, make ‘host’ name from ‘smtp.mailgun.org’ to nothing, so make it blank.
'host' => '',

2 Create Route
Go to your ‘routes.php’ and add
//Contact Page
Route::get('contact', 'ContactController@getContact');

//Form request:: POST action will trigger to controller
Route::post('contact_request','ContactController@getContactUsForm');


3 Create Controller to server Route request
Create new file inside ‘app/controller’: ContactController.php

class ContactController extends BaseController {

//Server Contact view:: we will create view in next step
public function getContact(){

return View::make('contact');
}

//Contact Form
public function getContactUsForm(){

//Get all the data and store it inside Store Variable
$data = Input::all();

//Validation rules
$rules = array (
'first_name' => 'required|alpha',
'last_name' => 'required|alpha',
'phone_number'=>'numeric|min:8',
'email' => 'required|email',
'message' => 'required|min:25'
);

//Validate data
$validator = Validator::make ($data, $rules);

//If everything is correct than run passes.
if ($validator -> passes()){

//Send email using Laravel send function
Mail::send('emails.hello', $data, function($message) use ($data)
{
//email 'From' field: Get users email add and name
$message->from($data['email'] , $data['first_name']);
//email 'To' field: cahnge this to emails that you want to be notified.
$message->to('me@gmail.com', 'my name')->cc('me@gmail.com')->subject('contact request');

});

return View::make('contact');
}else{
//return contact form with errors
return Redirect::to('/contact')->withErrors($validator);
}
}
}


4 Create a View inside ‘view/contact.blade.php’

<html>
<body>

{{ Form:: open(array('url' => 'contact_request')) }}

    @foreach($errors->all('
  • :message
  • ') as $message) {{ $message }} @endforeach

{{ Form:: label ('first_name', 'First Name*' )}}
{{ Form:: text ('first_name', '' )}}

{{ Form:: label ('last_name', 'Last Name*' )}}
{{ Form:: text ('last_name', '' )}}

{{ Form:: label ('phone_number', 'Phone Number' )}}
{{ Form:: text ('phone_number', '', array('placeholder' => '0280021xx')) }}

{{ Form:: label ('email', 'E-mail Address*') }}
{{ Form:: email ('email', '', array('placeholder' => 'me@example.com')) }}

{{ Form:: label ('subject', 'Subject') }}
{{ Form:: select ('subject', array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4'), '1' ) }}

{{ Form:: label ('message', 'Message*' )}}
{{ Form:: textarea ('message', '')}}

{{ Form::reset('Clear', array('class' => 'you css class for button')) }}
{{ Form::submit('Send', array('class' => 'you css class for button')) }}

{{ Form:: close() }}
</body>
</html>


5. Finally, we will create template to go with you email message
Go to ‘view/emails/’ and create new file ‘hello.blade.php’

<?!--This is a blade template that goes in email message to site administrator-->
<?php
//get the first name
$first_name = Input::get('first_name');
$last_name = Input::get ('last_name');
$phone_number = Input::get('phone_number');
$email = Input::get ('email');
$subject = Input::get ('subject');
$message = Input::get ('message');
$date_time = date("F j, Y, g:i a");
$userIpAddress = Request::getClientIp();
?>

<h1>We been contacted by.... </h1>

<p>
First name: <?php echo ($first_name); ?>
Last name: <?php echo($last_name);?>
Phone number: <?php echo($phone_number);?>
Email address: <?php echo ($email);?>
Subject: <?php echo ($subject); ?>
Message: <?php echo ($message);?>
Date: <?php echo($date_time);?>
User IP address: <?php echo($userIpAddress);?>

</p>



That's all, good luck.



Source: http://dixitpatel.com/form-in-laravel-with-email-function/
Official wiyono blog