php contact form w3schools code example

Example 1: php contact form

#Contact form

NOTE:  NEEDS An SMTP service on the website server.

<?php
    //Message Vars
    $msg = '';
    $msgClass = '';
    //check for the submit
    if(filter_has_var(INPUT_POST,'submit')){
    //Get form Data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    //Check Required Fields
    if(!empty($email) && !empty($name) && !empty($message)){
        //passed
        //check enail
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
            //Failed
            $msg = 'email format is incorrect';
            $msgClass='alert-danger';
        }else{
            //Passed
            //send to Recipient email needs an email host to send it
            $toEmail = '[email protected]';

        }
        
    }else{
        //failed
        $msg = 'Please Fill in all fields completely';
        $msgClass='alert-danger';
        //Email Subject
        $subject = 'contact request from '.$name;
        //creat body of the email
        $body = "<h2>Contact Request</h2>
        <h4>Name</h4><p>'.$name.'</p>
        <h4>Email</h4><p>'.$email.'</p>
        <h4>Message</h4><p>'.$message.'</p>";

        //Email Header
        $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";

        if(mail($toEmail, $subject, $body, $headers)){
            //Email sent
            $msg = 'Email sent';
            $msgClass = 'alert-success';

        }else{
            $msg = 'Email has not been sent';
            $msgClass = 'alert-danger';
    }
    }
?>

Example 2: php contact form script

<?php
  
if($_POST) {
    $visitor_name = "";
    $visitor_email = "";
    $email_title = "";
    $concerned_department = "";
    $visitor_message = "";
    $email_body = "<div>";
      
    if(isset($_POST['visitor_name'])) {
        $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Visitor Name:</b></label> <span>".$visitor_name."</span>
                        </div>";
    }
 
    if(isset($_POST['visitor_email'])) {
        $visitor_email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['visitor_email']);
        $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL);
        $email_body .= "<div>
                           <label><b>Visitor Email:</b></label> <span>".$visitor_email."</span>
                        </div>";
    }
      
    if(isset($_POST['email_title'])) {
        $email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Reason For Contacting Us:</b></label> <span>".$email_title."</span>
                        </div>";
    }
      
    if(isset($_POST['concerned_department'])) {
        $concerned_department = filter_var($_POST['concerned_department'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Concerned Department:</b></label> <span>".$concerned_department."</span>
                        </div>";
    }
      
    if(isset($_POST['visitor_message'])) {
        $visitor_message = htmlspecialchars($_POST['visitor_message']);
        $email_body .= "<div>
                           <label><b>Visitor Message:</b></label>
                           <div>".$visitor_message."</div>
                        </div>";
    }
      
    if($concerned_department == "billing") {
        $recipient = "[email protected]";
    }
    else if($concerned_department == "marketing") {
        $recipient = "[email protected]";
    }
    else if($concerned_department == "technical support") {
        $recipient = "[email protected]";
    }
    else {
        $recipient = "[email protected]";
    }
      
    $email_body .= "</div>";
 
    $headers  = 'MIME-Version: 1.0' . "\r\n"
    .'Content-type: text/html; charset=utf-8' . "\r\n"
    .'From: ' . $visitor_email . "\r\n";
      
    if(mail($recipient, $email_title, $email_body, $headers)) {
        echo "<p>Thank you for contacting us, $visitor_name. You will get a reply within 24 hours.</p>";
    } else {
        echo '<p>We are sorry but the email did not go through.</p>';
    }
      
} else {
    echo '<p>Something went wrong</p>';
}
?>

Tags:

Php Example