Please download this file public.rar and extract to your public folder (C:\wamp\www\test_laravel\public)
Step 1: Configure email setting.Go to edit this file C:\wamp\www\test_laravel\.env and update your email information.
Step 2: Go to edit this file C:\wamp\www\test_laravel\config\mail.php and update your sender information
Step 3: Edit route file (C:\wamp\www\test_laravel\app\Http\routes.php) and add
Route::controller('tutorial', 'TutorialController');
Step 4: Create 2 view files send_email.blade.php and mail_body.blade.php in C:\wamp\www\test_laravel\resources\views\tutorial (please create tutorial folder if not exist)
send_email.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Tutorials</title>
<!-- Styles -->
<link href="{{ asset('bootstrap-3.3.6/css/bootstrap.min.css') }}" rel="stylesheet">
</head>
<body>
<style>
.form-group.required .control-label:after {
content: " *";
color: red;
font-weight: bold;
}
</style>
<div class="container-fluid">
<div class="row"></div>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<br/>
@if(Session::has('success'))
<p class="alert alert-success"><b>Congratulation!</b> Your email was successfully sent</p>
@endif
<h2 class="page-header">Send Email</h2>
{!! Form::open(['class'=>'form-horizontal']) !!}
<div class="form-group required {{ $errors->has('email')?'has-error':'' }}">
{!! Form::label('email','Email',['class'=>'col-md-3 control-label']) !!}
<div class="col-md-8">
{!! Form::text("email",null,["class"=>"form-control"]) !!}
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group required {{ $errors->has('subject')?'has-error':'' }}">
{!! Form::label('subject','Subject',['class'=>'col-md-3 control-label']) !!}
<div class="col-md-8">
{!! Form::text("subject",null,["class"=>"form-control"]) !!}
@if ($errors->has('subject'))
<span class="help-block">
<strong>{{ $errors->first('subject') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group required {{ $errors->has('msg')?'has-error':'' }}">
{!! Form::label('msg','Message',['class'=>'col-md-3 control-label']) !!}
<div class="col-md-8">
{!! Form::textarea("msg",null,["class"=>"form-control"]) !!}
@if ($errors->has('msg'))
<span class="help-block">
<strong>{{ $errors->first('msg') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-push-3">
{!! Form::button("<i class='glyphicon glyphicon-floppy-disk'></i> Save",["type" => "submit","class"=>"btn
btn-primary"])!!}
</div>
</div>
{!! Form::close() !!}
</div>
<div class="col-md-3"></div>
</div>
</div>
<!-- JavaScripts -->
<script src="{{ asset('js/jquery-1.11.2.min.js') }}"></script>
<script src="{{ asset('bootstrap-3.3.6/js/bootstrap.min.js') }}"></script>
</body>
</html>
mail_body.blade.php
{!! nl2br($body) !!}
Step 6: Create controller file TutorialController.php in here C:\wamp\www\test_laravel\app\Http\Controllers
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
class TutorialController extends Controller
{
public function getSendEmail()
{
return view('tutorial.send_email');
}
public function postSendEmail(Request $request)
{
$validator = Validator::make($request->all(),
[
'email' => 'required|email',
'subject' => 'required',
'msg' => 'required'
]);
if ($validator->fails())
return back()->withErrors($validator)->withInput();
Mail::send('tutorial.mail_body', ['body' => $request->msg], function ($message) use ($request) {
$message->to($request->email)->subject($request->subject);
});
return back()->withSuccess(1);
}
}
How to test? open your browser and type http://localhost/test_laravel/public/tutorial/send-email. You will see this screen
No comments:
Post a Comment