File: //home/mirz4654/public_html/wp-content/themes/mira-yosomite/assets/php/contact.php
<?php
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "[email protected]";
function cleanData($data) {
return strip_tags(stripslashes(trim($data)));
}
function cleanEmail($email) {
return filter_var(cleanData($email), FILTER_SANITIZE_EMAIL);
}
function cleanMultiLines($data) {
return str_replace(array("\r","\n"), array(" "," "), $data);
}
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$fname = cleanMultiLines(cleanData($_POST["contactFname"]));
$lname = cleanMultiLines(cleanData($_POST["contactLname"]));
$email = cleanMultiLines(cleanEmail($_POST["contactEmail"]));
$subject = cleanMultiLines(cleanData($_POST["contactSubject"]));
$message = cleanData($_POST["contactMessage"]);
// Check that data was sent to the mailer.
if ( empty($fname) OR empty($lname) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
// Build the email content.
$email_content = "First Name: $fname\n";
$email_content .= "Last Name: $lname\n";
$email_content .= "Email Address: $email\n";
if (isset($_POST["contactPhone"])) {
$phone = strip_tags(trim($_POST["contactPhone"]));
$phone = str_replace(array("\r","\n"),array(" "," "),$phone);
if (!empty($phone)) {
$email_content .= "Phone Number: $phone\n";
}
}
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $fname $lname <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
/*$file = fopen('mail.txt', 'w');
fwrite($file, $email_content);
fclose($file);*/
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>