How to create an easy contact form using PHP

Every website requires contact form because it allows visitors to send messages to the site owner and visitors can easily submit their query OR opinion to website administrator.

To send this information i.e. contact form details PHP mail() function is used. Its very simple process visiter can easily fill contact form this information is directly he can send it to the website owner or administrator.

Here is HTML and CSS code of Contact form

Create a file with name contact.php and add below code into it –

<meta name="viewport" content="width=device-width, initial-scale=1">

	<style type="text/css">
		padding: 10%;
    	background: #e4d3b7;
		.contactForm h4 {
    font-size: 1em;
    color: #252525;
    margin-bottom: 0.5em;
    font-weight: 300;
    letter-spacing: 5px;
.contactForm input[type="text"], .contactForm input[type="email"] {
    width: 92%;
    color: #000;
    background: #fff;
    outline: none;
    font-size: 0.9em;
    padding: .7em 1em;
    border: 1px solid #da941c;
    -webkit-appearance: none;
    display: block;
    margin-bottom: 1.2em;
    width: 100%;
.contactForm textarea {
    resize: none;
    width: 93.5%;
    background: #fff;
    color: #000;
    font-size: 0.9em;
    outline: none;
    padding: .6em 1em;
    border: 1px solid #da941c;
    min-height: 10em;
    -webkit-appearance: none;
    width: 100%;
.contactForm input[type="submit"] {
    outline: none;
    color: #FFFFFF;
    padding: 0.5em 0;
    font-size: 1em;
    margin: 1em 0 0 0;
    -webkit-appearance: none;
    background: #da941c;
    transition: 0.5s all;
    border: 2px solid #da941c;
    -webkit-transition: 0.5s all;
    transition: 0.5s all;
    -moz-transition: 0.5s all;
    width: 100%;
    cursor: pointer;
.contactForm input[type="submit"]:hover {
    background: none;
    color: #000;
p.succdiv{color: #008000;}
<div class="contactForm">

	<h2 style="text-align: center;">Simple PHP Contact Form</h2>

    <form action="" method="post" class="c-form">
        <input type="text" name="name" placeholder="Your Name" required="">

        <input type="email" name="email" placeholder="Your Email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" title="Enter valid email"  required="">

        <input placeholder="Mobile Number"  type="text" name="contact" minlength="10" maxlength="10" pattern="[1-9]{1}[0-9]{9}" title="Enter 10 digit mobile number" required>
       <input type="text" name="subject" placeholder="Your subject" required="">
        <textarea id="subject" name="message" placeholder="Write  your message here" style="height:200px"></textarea>

        <input type="submit" name="submit" value="Submit">

Now you can see a simple Contact form HTML design on browser.

Below is PHP code too process this contact form, this PHP code will check if form is submitted or not and if submitted then validate Email using filter_var() function.

If everything workes properly then an email will be shooted from the code which will send form data to the respective email address.

$statusMsg = '';
$msgClass = '';
    // Get the submitted form data
    $email = $_POST['email'];
    $name = $_POST['name'];
    $contact = $_POST['contact'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    // Check whether submitted data is not empty
    if(!empty($email) && !empty($name) && !empty($subject) && !empty($message)){
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
            $statusMsg = 'Please enter your valid email.';
            $msgClass = 'errordiv';
            // Recipient email
            $toEmail = '';
            $emailSubject = 'Contact Request Submitted by '.$name;
            $htmlContent = '<h2>Contact Request Submitted</h2>
            // Set content-type header for sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            // Additional headers
            $headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";
            // Send email
                $statusMsg = 'Your contact request has been submitted successfully !';
                $msgClass = 'succdiv';
                $statusMsg = 'Your contact request submission failed, please try again.';
                $msgClass = 'errordiv';
        $statusMsg = 'Please fill all the fields.';
        $msgClass = 'errordiv';

Leave a Reply

Your email address will not be published. Required fields are marked *