Example 1: php contact form
#Contact form
NOTE: NEEDS An SMTP service on the website server.
//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 = "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($_POST) {
$visitor_name = "";
$visitor_email = "";
$email_title = "";
$concerned_department = "";
$visitor_message = "";
$email_body = "";
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 .= "";
$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 "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
';
}
?>