PHP contact form script

14 June 2009 | 4 Comments » | Mujtaba

The most common use of php on the internet is probably to make Contact Forms or mail forms . Here is a free php contact form script that takes input from 5 fields : name,email,url,subject and message. The script also validates the input and checks to see if the name , email and message fields are filled out properly and also checks if the email address provided is valid. the script then sends a mail to the administrator containing the message and the visitor info and also writes (appends) the input to a “contact.log” file . The script is quiet easy to understand and edit as it is commented and well written. Just place the following code in a “contact.php” file and change the value of $admin_mail to your email id. You can style the form to your taste using css.

<?php

error_reporting(0);
        //Replace with your email ID
        $admin_mail="mujtaba_91@yahoo.com";
       
if (isset($_POST[‘button’])) {
        $date=date(‘l dS \of F Y h:i A e’);
        $name=htmlentities($_POST[‘name’]);
        $email=htmlentities($_POST[‘email’]);
        $url=htmlentities($_POST[‘url’]);
        $subject=htmlentities($_POST[’subject’]);
        $message=htmlentities(stripslashes($_POST[‘message’]));
        //Form Validation
        $errors="";
        if(empty($name)) {
                $errors.="Please enter your name <br />";
        }
       
        if(empty($email) || !strpos($email, "@") || !strpos($email, ".")) {
                $errors.="Please enter a valid email ID <br />";
        }
       
        if(empty($message)) {
                $errors.="Please enter a message <br />";
        }
       
        if(!empty($url) && !strpos($url,"http://")) {
                $url="http://".$url;
        }
       
       
        //Building mail if the input is valid
       
        if ($errors=="") {
        $mail_subject="Message from :".$email;
         //Required for html formatted mail, do not remove
        $headers  = ‘MIME-Version: 1.0′ . "\r\n";
        //Required for html formatted mail, do not remove
        $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . "\r\n";
        $headers .= "To: Admin <".$admin_mail.">" . "\r\n";
        $headers .= "From: ".$name." <".$email.">" . "\r\n";
       
        //The mail
        $mail_message="
        <html>
        <head>
        <title> Message from "
.$name." </title>
        </head>
        <body>
        <b>Name</b> :<br /> $name <br />
        <b>Email</b> :<br /> $email <br />
        <b>URL</b> :<br /> <a href=\"$url\">$url</a> <br />
        <b>IP Address</b> : <br/>
        "
.$_SERVER[‘REMOTE_ADDR’]." <br />
        <b>Date and Time</b> : <br />
        $date <br />
        <b>Message</b> :<br/> "
.nl2br($message)."
        </body>
        </html>"
;
       
        if(mail($admin_mail,$mail_subject,$mail_message,$headers)) {
                $status="Mail Sent Successfully";
                //echo "<div class=’success’>$status</div>";
        }
        else {
                $status="Mail Failed to Send";
                //echo "<div class=’error’>$status</div>";
        }
       
        //Logging the message to a file
        $file="contact.log";
        $log_message="
        Name : $name\r\n
        Email : $email\r\n
        URL: $url\r\n
        Subject: $subject\r\n
        Time: $date\r\n
        IP Address: "
.$_SERVER[‘REMOTE_ADDR’]."\r\n
        Message:
$message\r\n
        Mailing Status: $status\r\n
        _______________________________________________________________________\r\n
        "
;
        $fh=fopen($file,"a+");
        fwrite($fh,$log_message);
        fclose($fh);
       
        echo "Thanx! i will get back to you soon";
        }
        else {
                echo $errors;
        }

       
}

else {
        echo "
        <form action=\"contact.php\" method=\"post\" id="
myform" >
        <input type=\"text\" name=\"name\" /> Name <br />
        <input type=\"text\" name=\"email\" /> Email <br />
        <input type=\"text\" name=\"url\" /> URL <br />
        <input type=\"text\" name=\"subject\" /> Subject <br />
        <textarea cols=\"30\" rows=\"20\" name=\"message\" ></textarea>
        <input type=\"submit\" name=\"button\" value=\"Submit\" />
        </form>"
;
}
?>