Last Updated on January 30, 2023 by Roshan Parihar
In this post, learn the PHP code for a contact form to send an email with validation. If you want to communicate with your audience, you have to collect your audience queries from contact forms via emails.
Creating a beautiful contact form using HTML and CSS is easier to design. However, sending emails containing data users fill on contact forms requires some PHP knowledge and PHP coding.
Looking to create contact forms and send emails without coding? Create beautiful-looking contact forms with JotForm.
If you are a beginner and just started learning PHP, you need to know the right way of writing PHP code to send emails from your contact form. You can easily capture audience queries and send emails with the PHP code given below.
So, let’s get started.
PHP Code For Contact Form to Send Email
You have to first design a contact form for your website using HTML and CSS. After that, store the form data in a variable and use the mail()
of PHP to send emails.
Let’s find out with the examples given below containing the PHP code for a contact form to send an email.
Contact Form Design Using HTML Code
Firstly, you have to create a contact form design for your website to allow your audience to fill out and submit. If you are a website owner, you need to create a contact form that can help you collect queries and make communication.
To make communication, you can add an input field to collect the email address of your audience. To get queries from your audience, add a text area message box where the audience can write their queries. For personal communication, a name field in the contact form is also useful for you.
So, below are the form fields we are going to add to the contact form:-
- Name input field
- Email input field
- Message box text area field
To create a simple contact form design, you can use a few HTML codes as given below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<div class="main-contact-form"> <form action="#" method="POST"> <label for="Name">Your Name</label> <input type="text" name="name" class="name" placeholder="Enter name" required> <label for="Email">Your Email</label> <input type="email" name="email" class="email" placeholder="Enter email" required> <label for="message">Message</label> <textarea name="message" class="message" placeholder="Enter your message" rows="5" required></textarea> <input name="submit" type="submit" value="Submit"> </form> </div> |
To make your contact form beautiful, you can add CSS code for styling as given below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<style> input[type='text'],input[type='email'], textarea{ width: 100%; padding: 10px; border-radius: 3px; border: 1px solid #ccc; margin-top: 10px; margin-bottom: 20px; } input[type='text']:focus,input[type='email']:focus, textarea:focus{ border: 1px solid #5db6db; box-shadow: 0 0 10px #b9eaff; outline: none !important; } input[type='submit']{ background: rgb(39,160,210); color: #fff; border: none; padding: 10px 20px; cursor: pointer; } .main-contact-form{ max-width: 400px; margin: 0 auto; background: #e9e9e9; padding: 20px 45px 20px 25px; border-radius: 5px; } </style> |
Now, after adding the above HTML code and CSS for styling, the final appearance of your contact is as shown in the image below.
Let’s add some PHP code for a contact form to send an email.
Send Email with Validation Using PHP Code
If you want to send emails from your contact form, you have to first get the form data using PHP global variable $_POST
. Use the name of the input field inside the $_POST[]>
to get form data.
As all the form fields in the HTML code above contain required
attribute. That’s why it’s not required to add validation to make all the fields required to fill. However, you need to add validation for an email address to collect a valid email address from your audience.
isset()
function to execute the PHP code only when your audience clicks the submit button. After that, you can use the mail()
as given in the PHP code below to send emails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php if(isset($_POST['submit'])){ //Get form data $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; //Email address validation and display error message $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if (!preg_match($email_exp, $email)) { echo '<br><span style="color:red;">The Email address you entered is not valid.</span>'; exit; } $subject = "Contact Form"; //Subject of the email //Message content to send in an email $message = "Name: ".$name."<br>Email: ".$email."<br> Message".$message; //Email headers $headers .= "Reply-To:".$email."\r\n"; //Send email mail($to, $subject, $message, $headers); } ?> |
Description of PHP Codes to Send Emails
Let’s understand each variable to send emails using PHP code:-
$to
: The recipient email address is the email address of the website owner who wants to get emails to their email inbox.$subject
: It is the subject of the email the recipient will receive.$message
: The main content body of the email you send to the recipient. You can add the data in this variable to get from the contact form.$headers
:- ‘From’ email is the email address of the website owner that should be domain specific.
- ‘CC’ is the email address of the second person who wants to receive the same email.
- “Reply-To’ is the email address of the audience who fills out the contact form.
mail()
: It is the main function in PHP to send emails. You have to pass the above 4 variables as an argument of the function to make it work in PHP.
You May Also Like to Read