Laravel contact form to mail
working step::
1) create controller
=====php artisan make:controller visitorcontactController
2) create form
=======
<div class="contact-form">
@if ($show = Session::get('message'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $show }}</strong>
</div>
@endif
<form action="{{url('visitor/contact')}}" method="POST">
@csrf
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" id="name" name="name" type="text" placeholder="Name*">
@if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
<input class="form-control{{ $errors->has('contact_phone') ? ' is-invalid' : '' }}" id="contact_phone" name="contact_phone" type="text" placeholder="Phone*">
@if ($errors->has('contact_phone'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact_phone') }}</strong>
</span>
@endif
<input class="form-control{{ $errors->has('contact_email') ? ' is-invalid' : '' }}" id="contact_email" name="contact_email" type="text" placeholder="Email">
@if ($errors->has('contact_email'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact_email') }}</strong>
</span>
@endif
<textarea class="form-control{{ $errors->has('contact_text') ? ' is-invalid' : '' }}" rows="8" name="contact_text" placeholder="Message*"></textarea><br/>
@if ($errors->has('contact_text'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact_text') }}</strong>
</span>
@endif
<input id="contact-btn" type="submit" name="contact_btn" value="submit">
</form>
</div>
3) create web
=======
Route::post('visitor/contact', 'visitorcontactController@visitorcontact');
4) create controller
use App\Post;
use Mail;
public function visitorcontact(Request $request){
$this->validate($request, [
'name'=>'required',
'contact_email'=>'required',
'contact_text'=>'required',
]);
$data = array(
'name' => $request->name,
'contact_email' => $request->contact_email,
'contact_phone' => $request->contact_phone,
'contact_text' => $request->contact_text,
);
$send = Mail::send('backEnd.emails.email', $data, function($textmsg) use ($data){
$textmsg->from($data['contact_email']);
$textmsg->to('zadumia441@gmail.com');
$textmsg->subject($data['contact_text']);
});
if($send){
return redirect('/contact')->with('message', 'Message sent successfully!');
}else{
return redirect('/contact')->with('message', 'Message sent successfully!');
}
}
5) edit env
======
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=demo@gmail.com
MAIL_PASSWORD=*************
MAIL_ENCRYPTION=tls
[Note : when upload your project on online please edit mail driver
MAIL_DRIVER= mail
]
6) create message view body email.blade.php
<h3>You have a new massage form Kcirque IT website</h3>
<p><b>Name :</b> {{$name}}</p>
<p><b>Phone :</b> {{$contact_phone}}</p>
<p><b>Sent Via :</b>{{$contact_email}}</p>
<b>Visitor says :</b>{{$contact_text}}
1) create controller
=====php artisan make:controller visitorcontactController
2) create form
=======
<div class="contact-form">
@if ($show = Session::get('message'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $show }}</strong>
</div>
@endif
<form action="{{url('visitor/contact')}}" method="POST">
@csrf
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" id="name" name="name" type="text" placeholder="Name*">
@if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
<input class="form-control{{ $errors->has('contact_phone') ? ' is-invalid' : '' }}" id="contact_phone" name="contact_phone" type="text" placeholder="Phone*">
@if ($errors->has('contact_phone'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact_phone') }}</strong>
</span>
@endif
<input class="form-control{{ $errors->has('contact_email') ? ' is-invalid' : '' }}" id="contact_email" name="contact_email" type="text" placeholder="Email">
@if ($errors->has('contact_email'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact_email') }}</strong>
</span>
@endif
<textarea class="form-control{{ $errors->has('contact_text') ? ' is-invalid' : '' }}" rows="8" name="contact_text" placeholder="Message*"></textarea><br/>
@if ($errors->has('contact_text'))
<span class="invalid-feedback">
<strong>{{ $errors->first('contact_text') }}</strong>
</span>
@endif
<input id="contact-btn" type="submit" name="contact_btn" value="submit">
</form>
</div>
3) create web
=======
Route::post('visitor/contact', 'visitorcontactController@visitorcontact');
4) create controller
use App\Post;
use Mail;
public function visitorcontact(Request $request){
$this->validate($request, [
'name'=>'required',
'contact_email'=>'required',
'contact_text'=>'required',
]);
$data = array(
'name' => $request->name,
'contact_email' => $request->contact_email,
'contact_phone' => $request->contact_phone,
'contact_text' => $request->contact_text,
);
$send = Mail::send('backEnd.emails.email', $data, function($textmsg) use ($data){
$textmsg->from($data['contact_email']);
$textmsg->to('zadumia441@gmail.com');
$textmsg->subject($data['contact_text']);
});
if($send){
return redirect('/contact')->with('message', 'Message sent successfully!');
}else{
return redirect('/contact')->with('message', 'Message sent successfully!');
}
}
5) edit env
======
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=demo@gmail.com
MAIL_PASSWORD=*************
MAIL_ENCRYPTION=tls
[Note : when upload your project on online please edit mail driver
MAIL_DRIVER= mail
]
6) create message view body email.blade.php
<h3>You have a new massage form Kcirque IT website</h3>
<p><b>Name :</b> {{$name}}</p>
<p><b>Phone :</b> {{$contact_phone}}</p>
<p><b>Sent Via :</b>{{$contact_email}}</p>
<b>Visitor says :</b>{{$contact_text}}
Comments
Post a Comment