Create a Contact Form in Laravel that Sends You an Email

by Saquib Rizwan  June 22, 2017

Laravel is well known for providing multiple solutions for a problem. This is one of the main reasons of the popularity of the framework. Popular Laravel solutions cover routine functionality such as authentication, sessions, routing, and caching. laravel contact form Contact Us forms are another routine functionality that is a requirement of more or less every website. In this article, I am going to demonstrate how you can easily create a contact form in Laravel with email. To understand the functionality of Laravel mail function. I suggest you read my previous article on sending emails in Laravel.

Install Laravel

To quickly setup a Laravel application, sign up at Cloudways for free. Login to your account and create a new server. Fill in the server and the application detail and select Laravel as your application. That’s it, you have just installed a new application within a few clicks on the Cloudways platform.

Install Form Package

To create forms in Laravel, I will use laravelcollective/html package for Form facade. In order to do that, go to your Cloudways platform and open the server tab. Launch the SSH terminal and login to your server using the Master Credentials. Now go to the root of your application by typing following commands.

After successful installation of the package, goto config/app.php file and add service provider and alias:

Configure the Database

Now that you have installed the package, the next step is database configuration. Open the Application tab in the Cloudways platform. In the application access detail, you will be able to see the database credentials for your application. Now go to the .env file, located in the application public root folder and add the credentials there.

Create Contact us Table Using Database Migrations

Go to the public root folder of your application and run the following command to create the database migration for creating the Contact Us model.

Now that the migration has been created, go to database/migration/date_ create_contact_us_table.php and add the following code in it.

Now run the following command

Now that the migration has been created, let’s create the Contact us model.

Create the Model

Go to your public root folder of your application and type the following command:

Now that you have successfully created the ContactUS model, go to app/ContactUS.php and add the following code in the file:

Next, I will create the route.

Create the Route

Go to routes/web.php and add the following code to the route:

Create the Controller

After setting the routes, I will create a controller to handle the requests from the routes. Now go to app/Http/Controllers/ContactUSController.php and add the following code in it.

Create the View

Everything is now ready and I will now create the layout of the form.  Go to resources/views/ and create a file contactUS.blade.php. Place the following code in it:

The contact form is now functional. On submitting the form, the data will be stored in the database. I will now make the form email the information.

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

Sending the Email

First read my previous article on sending email in Laravel.  Now that you have read the steps, it is time to implement the process. First, setup the basic Laravel mail function through PHP Artisan. Use the following command in the terminal:

After that, login to your Gmail account. Under My account > Sign In And Security > Sign In to Google, enable two-step verification, and then generate the app password. Next, add the app password in the  .env file with the following configurations.

Now that the configuration is done, update ContactUsController.php with the following code.

The contact form is now ready for deployment. It will save the data in the database and email it to the given email address as well. To test the Contact Us form, go to the application and click on launch application (remember to add /contact-us to the URL).   Check the demo to see the application in action.

The Final Words

In this tutorial, I have demonstrated how to create a contact form in Laravel with the functionality of emailing the information to a prespecified email address. Sign up at Cloudways for awesome Laravel hosting and tutorials.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Saquib Rizwan

Saquib is a PHP Community Expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at saquib.rizwan@cloudways.com

Stay Connected:

You Might Also Like...