In this post, learn how to create contact form in PHP and send form data in email. The short answer is to use the PHPMailer function to send emails in PHP.
Every website needs a contact form to receive user’s input data to their email id. Contact forms are the best option to connect with your audience and
You have to first create a complete contact form with fields like name, email, subject, and message. When the user submits the form, it should be first validated to check the fields are not empty and the email format is correct.
After the form gets validated and the user enters valid data, you have to use the PHP code with PHPMailer to send form data in email. The best option to send emails is to use the Gmail SMTP to confirm sending of emails.
In this post, you will learn the step-by-step process of how to create a contact form and email form data. So, let’s get started.
How to Create Contact Form in PHP and Send Email
Let’s start with creating a contact form in PHP with standard form fields.
Step 1: Create a Contact Form in PHP
Here are the form fields including name, email, subject, and message with a submit button. Also, add action
and method
attribute. The action
contains the value as the location PHP page where you have placed your PHP codes. The method
attribute contains the value ‘POST’ as you have to post form data on submission.
Step 2: Validate Your Contact Form Using jQuery
After you have created your form, you have to validate your form data to collect valid input from users. You can use jQuery for form validation as given below. It first checks whether the form fields are empty or not. If the form fields are empty, it will not allow users to proceed.
It also checks the email id input if its format is correctly entered by the users or not. If the email format is not correct, it will also do not allow users to submit the form.
Don’t forget to match the class of the form fields with the above variable and the submit button. After form validation, you can proceed further to collect user’s input data and send emails using PHP.
Step 3: Email Form Data to the Recipient Using PHPMailer
First of all, download the PHPMailer file on the base location where you have placed your PHP code file.
Now, you can create a PHP code to collect form data and email them to the receiver. First, you have to check if the form submitted uses the PHP isset() function as shown below.
If the form is submitted, its data are collected in a separate variable. After that, it uses the SMTP and PHPMailer to send emails as shown in the PHP code below:-
You have to place the above PHP code inside a separate PHP file. After that, add the attribute action="post.php"
inside the <form>
tag. The ‘post.php’ file contains the above PHP code to execute on form submission.
Now, your contact form is ready to get submitted and send emails to the recipients. You can add as many recipients as you want.
You May Also Like to Read
- Ajax Image Upload Using PHP, jQuery With Image Preview
- How to Convert or Change Date Format to Another in PHP
- How to Use CSS in PHP Echo to Add Style (3 Easy Ways)
I hope you like this post on how to create a contact form in PHP and send emails.