Categories: Laravel Tutorial: Step by Step Guide to Building Your First Laravel Application - Techanical-Atom

Create Contact Form In Laravel That Sends Email6 min read

Create Contact Form In Laravel That Sends Email
Reading Time: 4 minutes

Create Contact Form In Laravel That Sends Email:- Laravel is known to provide multiple solutions to the common problem faced in the developing an application. That is one the many reason why Laravel is the most popular framework of php around us whether it is authentication, caching, sessions or routing Laravel will handle them all very efficiently.

Contact Form In Laravel are also such functionality which are present in almost all the websites. Now today in this article i am going to demonstrate how you can create a contact form in Laravel with the information being stored in the database as well as an email with the intended information will be sent to the to a specified email address.

Create Contact Form In Laravel That Sends Email

Contact Form In Laravel

Now before starting, I suggest you read my previous article Laravel Tutorials For Beginners Step By Step Guide in which i have briefly explained how you can install Laravel. After quickly setting up the Laravel application now it’s time to configure your database. Quickly go to your .env file in the located in public root folder of the laravel application and add the following line of code into the file.

 Create Contact Us table using the migrations

Now open command prompt and go to the public root folder of your application and run the following command to create the migration for your contact form table.

After migrations commands runs successfully go to the database/migration/date_ create_contact_us_table.php  and edit the file as follows to create the table schema.

Now as your migration is ready for migrating run the following command to migrate the migration.

 Create the Model

After creating the migration now its time to create the model for the newly created contactus table . To create the model run the following command into the command prompt.

After that go to the App\ContactUs.php and edit the file with the following code as shown below.

 Create the routes

Now it’s time to create the route for the contact form. Head to the routes/web.php file and add the  lines of the code into your web.php file

Creating the controller for the form

Now all the logic for our contact form will be placed inside the controller file. Heads to the app/Http/Controllers/ContactUsController.php and edit the file as follows.

In the controller , we have placed the code for inserting the data into the database table that we have created earlier and below that is the code for the mailer to send the mail to prespecified user.Next, add the app password in the  .env file with the following configurations.

 Creating the Blade files for the form and email

Now as our logic is ready Go to resources/views/ and create a file contactUs.blade.php. Place the following code in it:

Now create another view, email.blade.php and add the following code in it.

Final Words

Now the contact form is ready for deployment with functionality of emailing the information to a prespecified email address. Laravel powered Contact Us form has a unique feature – it sends out emails for every Submit action, allowing you to get instant notifications for your form. This is a great addition to any project that needs a contact form.If you find this article useful then gives us thumbs up .Till then

Cheers ! Happy Coding

All Rights Reserved @ Techanical-Atom
%d bloggers like this: