Laravel 6 contact form tutorial

Laravel 6 contact form tutorial

Contact form is most often an important part of any website. If you are a new Laravel developer then this post will help you to learn a step by step process for how to make a contact us form and submit for sending an e-mail to your desired email address. To send e-mail from a contact form we need to know about how to send e-mail in Laravel framework. In Laravel framework, e-mail sending is really simple. We can send e-mail in various way with same e-mail API that Laravel provides. Before we start, I'll recommend you to read a previous post on sending email laravel. Let's make a contact form in using Laravel so that user can send an email by our website contact form.

 

Table of Content

  • Make routes
  • Create a contact form
  • Make a controller for handle form request
  • Contact form validation
  • Mail configuration
  • Test the contact form

 

Make routes

Let's define 2 routes in web.php file in routes directory of Laravel project.

Route::get('contact-us','ContactUsController@index');
Route::post('contact-us','ContactUsController@handleForm');

 

Create a contact form

Now make a contact.blade.php file in resources/views folder and do the contact form markup.

<form action="{{ ur1('contact-us')" method="post"› 

    @csrf
    <div class="form-group"›
       <label>Name</label>
       <input type="text" name="name" class="form-contror"> 
    </div>
	
    <div class="form-group"›
       <label>E-mail</label>
       <input type="email " name="email " class="form-control">
    </div>
	
    <div class="form-group"›
       <label>Message</label>
       <textarea name="message_body" class="form-control"></textarea>
    </div>
	
    <button type="submit" class="btn btn-primary">Submit</button>

</form>

 

Make a controller for handle form request

Make a contact form handler controller via artisan command.

php artisan make:controller ContactUsController

 

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Mail;

class ContactUsController extends Controller
{
    public function index()
    {
        return view('contact');
    }

    public function handleForm(Request $request)
    {

        $this->validate($request, ['name' => 'required', 'email' => 'required|email', 'message_body' => 'required|min:20']);

        $data = ['name' => $request->get('name') , 'email' => $request->get('email') , 'messageBody' => $request->get('message_body') ];

        Mail::send('email', $data, function ($message) use ($data)
        {
            $message->from($data['email'], $data['name']);
            $message->to('info@laravelarticle.com', 'Admin')
                ->subject('Laravel Article Feedback');
        });

        return redirect()
            ->back()
            ->with('success', 'Thank you for your feedback')

    }

}

 

Mail Configuration

Set mail credentials on env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME= email_address
MAIL_PASSWORD= email_pass
MAIL_ENCRYPTION=tls

create an e-mail template for sending it on mail. Create a blade file name with email.blade.php in resources/views/emails

Message from : {{ $name }}
<p> Name: {{ $name }}</p>
<p>E-mail: {{ $email }}</p>
<p>Message: {{ $messageBody }}</p>

 

Test the contact form

Now browse http://example.com/contact-us and fill up the name, email, message and submit the form. If you set up everything correctly then you will get an e-mail into your mail address.

 

Conclusion

Thank you for reading this tutorial post with patience. I hope, this tutorial post will help you to make contact feedback form on your website. If this post is helpful to you then please share it with others.

 

Share

Related Post


Laravel Simple Filemanager

Speed Up Laravel website - Step by Step

Repository Design Pattern in Laravel

Laravel desktop application using Electron Js

Top 10 question-answer for New Laravel developer

Laravel Excel import export