2018-11-25 Pujan Niroula 2 Minute(s) Read

How To Make Simple Contact Us form in PHP

img-secure contact us.png

Most blogs or website have contact us form in their page. That will admin to directly communicate with visitors. Usually newbie bloggers don't make contact us form in their blog. If you also don't have contact us form then you may be loosing some loyal visitor or good deal.

Let's Create Contact Us Form in PHP that will send mail to you and the person who contacted you when someone submit this form.

Simple and Secure Contact Us Form in PHP

This code have following features,

  1. Validating empty value.
  2. Validating email.
  3. Validating on Frontend and Backend.
  4. Mail to admin as well as user.

Frontend Part,

<form method="post" action="#">
<input name="name" pattern="[a-z\.' ']" required="" type="text" placeholder="Full Name" />
<input name="subject" required="" type="text" placeholder="Subject" />
<input name="email" required="" type="email" placeholder="Email" />
<textarea name="message" placeholder="Message" required=""></textarea>
<input type="submit" name="send" value="Send Message">
</form>

Let's go to backend

We will use mail() function in PHP to deliver E-mail you just have to change variable $youremail to your private email and  email to receive mail and change $publicemail to your public email from which you send mail to user.

<?php
if(isset($_POST['send']))
{
    $youremail = "youremail@mail.com.np";
    $publicemail = "your_public_email@mail.com.np";
    function input($name){
        if(isset($_POST[$name]) && !empty(trim($_POST[$name]))){
            $input = trim($_POST[$name]);
            $html = htmlspecialchars($input);
            return $html;
        } else {
            return false;
        }
    }
    if(input('name') && input('subject') && input('message') && input('email')){
        $name = input($_POST['name']);
        $subject = input($_POST['subject']);
        $message = input($_POST['message']);
        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            $email = $_POST['email'];
        } else {
            $err = 'Email format is not correct';
        }
    }
    else{
        $err = 'Please input all fields correctly';
    }
    if(!isset($err)){    
    //Send it to you
        $header = 'From: .'.'$name<'.$email.">\r\nReturn-path: ".$email;
        $subject = 'New Message from Contact Form';
        $send = mail($youremail, $subject, $message, $header);
    //To send thanks mail
        if ($send) {
            $header = 'From: .'.'$name<'.$publicemail.">\r\nReturn-path: ".$publicemail;
            $subject = 'Email Received Conformation';
            $message = 'We have received your message from contact us form. We will get back to you as soon as possible';
            mail($email, 'Email', $message, $header);
        }
    }
}
?>

If you want full code download it from below...

I will be happy to hear from you.

Download Attached Files: Contact Us Script in PHP.zip
Comments (0) Add New Comment
No comments, Be first one to commment!