php contact form script code example
Example 1: php contact form
#Contact form
NOTE: NEEDS An SMTP service on the website server.
Contact Request
Name
'.$name.'
Email
'.$email.'
Message
'.$message.'
";
//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
";
if(isset($_POST['visitor_name'])) {
$visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
$email_body .= "
".$visitor_name."
";
}
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 .= "
".$visitor_email."
";
}
if(isset($_POST['email_title'])) {
$email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING);
$email_body .= "
".$email_title."
";
}
if(isset($_POST['concerned_department'])) {
$concerned_department = filter_var($_POST['concerned_department'], FILTER_SANITIZE_STRING);
$email_body .= "
".$concerned_department."
";
}
if(isset($_POST['visitor_message'])) {
$visitor_message = htmlspecialchars($_POST['visitor_message']);
$email_body .= "
".$visitor_message."
";
}
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 .= "
Thank you for contacting us, $visitor_name. You will get a reply within 24 hours.
"; } else { echo 'We are sorry but the email did not go through.
'; } } else { echo 'Something went wrong
'; } ?>