Make Contact Form in Laravel

Here are some steps to create contact form:

Step1

To make contact form in laravel, first open terminal and install laravel project using composer command:
composer create-project –prefer-dist laravel/laravel Project

Step2

After installation of laravel, go to project directory and create a controller for handling http request:
php artisan make:controller ControllerName
For example:

This command has make SendEmailController under app folder directory: “app/Http/Controller”

After open “SendEmailController.php file”, we have make index function which is root function of this controller and under this function we have write return statement with view method and under this method we have write view file(“send_email”).

Step3

After write a method of view file, create a view file under resources folder and under view folder.

Under this send_email.blade.php file we have made one form for send an email to controller and under this file we have “csrf_field” function for prevent the application from cross side request forgery attack. In this file also we have create the GUI such as text box for store details of name, create text box for store email and create the text area for store the message.

Under this file we have write the some block of code for creating gui, and validating condition and write code to show success message under condition if form is successfully submitted.

Step4

After writing GUI and validation message in the view file, go to SendEmailController file and create method for validating data against form data.

Under this controller class we have defined the Mail class and create mail statement, for this we can use laravel mailer class. After that we have write some block of code for validating the template class.

Step5

After validating data, we go to route folder and web.php file where we setup the route of both method SendEmailController and send_email.blade.php files.

Under this file we write route class with get method with two arguments: “/sendemail” and “SendEmailController@index”, so when in browser we have type or paste the url “/sendemail” then it will call index method of SendEmailController index.

And we will route class with post method for send method with two arguments: “/sendemail/send” and “SendEmailController@send”, so when in browser form is submitted and send request to send method then it will execute send method of send email controller.

Step6

Now we have proceeded for send email, so we have go to terminal (command prompt)and first stop laravel server and type command: “php artisan make:mail SendMail”

This command will make Mailable class with name like SendMail under this mail folder in directory “app/Mail/SendMail.php”. In build function we have make dynamic subject which show the name of user who fill the form. And we have to create “dynamic.blade.php” file under the view folder. Under this dynamic php file the email body has taken.

After the completion of SendMail file we set the variable under the dynamic.blade.php file we store the user name data and show the message query.

Step 7

After completed the all php file, now we have set the configuration body for send email from laravel application. For this we have go to “.env” file and here mail driver option for send email. In this env file first we have to set username and password. The user name and password we get from the https://mailtrap.io/.

To getting the mail username and password we have create an id and after the id created then we have go to the login option. After the login we can see the user name and password:

Results

After the completion of all file we have start your server, using server artisan command: “php artisan serve”.
After run this command this will give you an address of server.

Then go to your browser and type address “http://127.0.0.1:8000/sendemail”.

To check validation:

Successfully submitting form

Check sender’s mail: Go to you mailtrap.io and check into your mailbox list.

Leave a comment

Your email address will not be published. Required fields are marked *