[ 'to_email' => 'contact@landlines.tech' ] ]; function generate_captcha() { $num1 = rand(1, 10); $num2 = rand(1, 10); $_SESSION['captcha_answer'] = $num1 + $num2; return "What is $num1 + $num2?"; } function check_honeypot() { if (filter_has_var(INPUT_POST, 'nickname')) { $honeypot = trim($_POST['nickname']); if ($honeypot !== '') { header($_SERVER['SERVER_PROTOCOL'] . ' 405 Method Not Allowed'); exit; } } } function send_email($inputs, $recipient_email) { $name = htmlspecialchars($inputs['name']); $org = htmlspecialchars($inputs['organization']); $email = htmlspecialchars($inputs['email']); $message = nl2br(htmlspecialchars($inputs['message'])); $subject = "Contact Form: $name ($org)"; $body = <<

Name: $name

Organization: $org

Email: $email

Message:

$message

HTML; $headers = [ 'MIME-Version: 1.0', 'Content-type: text/html; charset=utf-8', "To: $recipient_email", "From: servers@landlines.tech", "Reply-To: $email" ]; $header = implode("\r\n", $headers); return mail($recipient_email, $subject, $body, $header); } function validate() { $inputs = []; $errors = []; // validate name if (filter_has_var(INPUT_POST, 'name')) { $inputs['name'] = trim($_POST['name']); if ($inputs['name'] === '') { $errors['name'] = 'Please enter your name'; } } else { $errors['name'] = 'Please enter your name'; } // validate organization if (filter_has_var(INPUT_POST, 'organization')) { $inputs['organization'] = trim($_POST['organization']); if ($inputs['organization'] === '') { $errors['organization'] = 'Please enter your organization'; } } else { $errors['organization'] = 'Please enter your organization'; } // validate email if (filter_has_var(INPUT_POST, 'email')) { $inputs['email'] = trim($_POST['email']); if (!filter_var($inputs['email'], FILTER_VALIDATE_EMAIL)) { $errors['email'] = 'Please enter a valid email address'; } } else { $errors['email'] = 'Please enter your email address'; } // validate message if (filter_has_var(INPUT_POST, 'message')) { $inputs['message'] = trim($_POST['message']); if ($inputs['message'] === '') { $errors['message'] = 'Please enter a message'; } } else { $errors['message'] = 'Please enter a message'; } // validate captcha if (filter_has_var(INPUT_POST, 'captcha')) { $captcha = trim($_POST['captcha']); if ($captcha === '') { $errors['captcha'] = 'Please answer the security question'; } elseif (!isset($_SESSION['captcha_answer']) || (int)$captcha !== $_SESSION['captcha_answer']) { $errors['captcha'] = 'Incorrect answer, please try again'; } } else { $errors['captcha'] = 'Please answer the security question'; } return [$inputs, $errors]; } $request_method = $_SERVER['REQUEST_METHOD']; if ($request_method === 'POST') { check_honeypot(); [$inputs, $errors] = validate(); if (empty($errors)) { send_email($inputs, $config['mail']['to_email']); $_SESSION['success_message'] = 'Thanks for contacting us! We will be in touch with you shortly.'; unset($_SESSION['captcha_answer']); } else { $_SESSION['error_message'] = 'Please fix the following errors'; $_SESSION['errors'] = $errors; $_SESSION['inputs'] = $inputs; } header('Location: ' . $_SERVER['PHP_SELF'], true, 303); exit; } if ($request_method === 'GET') { if (isset($_SESSION['success_message'])) { $success_message = $_SESSION['success_message']; unset($_SESSION['success_message']); } elseif (isset($_SESSION['inputs'], $_SESSION['errors'])) { $error_message = $_SESSION['error_message']; $errors = $_SESSION['errors']; $inputs = $_SESSION['inputs']; unset($_SESSION['errors'], $_SESSION['inputs'], $_SESSION['error_message']); } $captcha_question = generate_captcha(); } ?>

Contact Us