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 (6) Add New Comment
sklep2024-03-02 6:28 PM

Very nice post. I definitely love this site. Continue the good work! I saw similar here: e-commerce and also here: sklep

Reply
sklep online2024-03-02 6:21 PM

I was able to find good info from your content. I saw similar here: e-commerce and also here: sklep online

Reply
najlepszy sklep2024-03-02 1:22 PM

Thank you a lot for sharing this with all people you really recognize what you're talking about! Bookmarked. Please also consult with my site =). We could have a hyperlink exchange arrangement between us I saw similar here: Dobry sklep and also here: E-commerce

Reply
najlepszy sklep2024-02-24 8:55 PM

Wow, wonderful weblog layout! How lengthy have you ever been blogging for? you make blogging look easy. The whole glance of your website is great, let alone the content material! You can see similar: ecommerce and here ecommerce

Reply
Tutoring online2024-02-24 6:47 PM

Quality articles is the important to be a focus for the users to visit the web page, that's what this website is providing.

Reply
App Software Providers2024-02-23 12:07 PM

Hi, I do believe this is an excellent blog. I stumbledupon it ;) I'm going to revisit once again since I bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.

Reply