Laravel 9 Contact US Form with Send Email Example

https://www.itsolutionstuff.com/post/laravel-9-contact-us-form-with-send-email-exampleexample.html

Laravel 9 Contact US Form with Send Email Example

By Hardik Savani May 19, 2022 Category : LaravelPlayUnmuteLoaded: 0.16%FullscreenVDO.AIHello all! In this article, we will talk about laravel 9 contact form send email. let’s discuss about contact us form in laravel 9. step by step explain contact form validation in laravel 9. This article goes in detailed on laravel 9 feedback form example.

In this example, we will create a contact us form with name, email, phone, subject, and message. we will also add form validation for the contact form. After submitting the form we will store data in the database and send an email notification to the admin email. so let's follow the below step to make this example.

Preview:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

Step 2: Database Configuration

In second step, we will make database configuration for example database name, username, password etc for our crud application of laravel. So let's open .env file and fill all details like as bellow:

.env

Read Also: Laravel 9 Send Email using Queue Example

Step 3: Create Migration

we are going to create contact us form. so we have to create migration for "contacts" table using Laravel 9 php artisan command, so first fire bellow command:

After this command you will find one file in following path "database/migrations" and you have to put bellow code in your migration file for create products table.

Now you have to run this migration by following command:

Step 4: Create Model

In this step, we will create Contact.php model and i will create send email code on created event. so in last step we will create email class and template.

let's copy below code and paste it.

app/Models/Contact.php

Step 5: Create Routes

In this step, we will create two routes GET and POST. so let's add it.

routes/web.php

Step 6: Create Controller

In this step, we have to create new controller as ContactController and we have also need to add two methods index() and store() on that controller. We will use validation on store method. so let's update follow code:

app/Http/Controllers/ContactController.php

Step 7: Create View File

In Last step, let's create contactForm.blade.php(resources/views/contactForm.blade.php) for create form with display validation message and put following code:

resources/views/contactForm.blade.php

Step 8: Create Mail Class

In this step we will create mail class ContactMail for email send on contact us form create. So let's run bellow command.

now, let's update code on ContactMail.php file as bellow:

app/Mail/ContactMail.php

Here, we will create blade view file for email. In this file we will write all contact form details. now we just write some dummy text. create bellow files on "emails" folder.

resources/views/emails/contact.blade.php

next, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel will use those sender configuration for sending email. So you can simply add as like following.

.env

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

Now, Go to your web browser, type the given URL and view the app output:

Read Also: Laravel 9 Razorpay Payment Gateway Integration Example

Output:

Output: You Email Look Like This

I hope it can help you...

Last updated

Was this helpful?