working contact form php code code example
Example: php contact form
#Contact form
NOTE: NEEDS An SMTP service on the website server.
<?php
$msg = '';
$msgClass = '';
if(filter_has_var(INPUT_POST,'submit')){
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
if(!empty($email) && !empty($name) && !empty($message)){
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$msg = 'email format is incorrect';
$msgClass='alert-danger';
}else{
$toEmail = '[email protected]';
}
}else{
$msg = 'Please Fill in all fields completely';
$msgClass='alert-danger';
$subject = 'contact request from '.$name;
$body = "<h2>Contact Request</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Message</h4><p>'.$message.'</p>";
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-Type:text/html;charset=UTF-8" . "/r/n";
$headers.= "From: ".$name."<" .$email. ">". "\r\n";
if(mail($toEmail, $subject, $body, $headers)){
$msg = 'Email sent';
$msgClass = 'alert-success';
}else{
$msg = 'Email has not been sent';
$msgClass = 'alert-danger';
}
}
?>