<?php # Include the Google reCAPTCHA Library require_once('../../includes/google-recaptcha-library.php'); $g_recaptcha_key_site = '6Lf4FD8UAAAAAMSHjDrFao5IjTfmLsZr0buO9qbC'; $g_recaptcha_key_secret = '6Lf4FD8UAAAAANv28fBKN0BgKEkVQf4q8LSSiWNu'; # Add Google reCAPTCHA JavaScript to HTML HEAD $js_head = '<script src="https://www.google.com/recaptcha/api.js"></script>';
$errors = []; $missing = []; // check if the form has been submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { // email processing script $to = 'robert@nwtc-web.com'; // use your own email address $subject = 'Feedback from Japan Journey'; // list expected fields $expected = ['name', 'email', 'comments']; // set required fields $required = ['name', 'email', 'comments']; // create additional headers $headers[] = 'From: Japan Journey<robert.buchholz@nwtc.edu>'; $headers[] = 'Content-Type: text/plain; charset=utf-8';
// Place results of g_recaptcha_request() in $g_recaptcha_response $g_recaptcha_response = g_recaptcha_request(); //var_dump($g_recaptcha_response);exit; if (!$g_recaptcha_response->success) { $errors['recaptcha'] = true; }
# Not loading via include - showing here for demo only... // require('processmail_05.php'); // pattern to locate suspect phrases $pattern = '/[\s\r\n]|Content-Type:|Bcc:|Cc:/i'; // check the submitted email address $suspect = preg_match($pattern, $_POST['email']);
if (!$suspect) { foreach ($_POST as $key => $value) { // strip whitespace from $value if not an array if (!is_array($value)) { $value = trim($value); } if (!in_array($key, $expected)) { // ignore the value, it's not in $expected continue; } if (in_array($key, $required) && empty($value)) { // required value is missing $missing[] = $key; $$key = ""; continue; } $$key = $value; } } // validate the user's email if (!$suspect && !empty($email)) { $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); if ($validemail) { $headers[] = "Reply-To: $validemail"; } else { $errors['email'] = true; } } $mailSent = false; // go ahead only if not suspect, all required fields OK, and no errors if (!$suspect && !$missing && !$errors) { // initialize the $message variable $message = ''; // loop through the $expected array foreach ($expected as $item) { // assign the value of the current item to $val if (isset($$item) && !empty($$item)) { $val = $$item; } else { // if it has no value, assign 'Not selected' $val = 'Not selected'; } // if an array, expand as comma-separated string if (is_array($val)) { $val = implode(', ', $val); } // replace underscores in the label with spaces $item = str_replace('_', ' ', $item); // add label and value to the message body $message .= ucfirst($item) . ": $val\r\n\r\n"; } // limit line length to 70 characters $message = wordwrap($message, 70); // format headers as a single string $headers = implode("\r\n", $headers); $mailSent = mail($to, $subject, $message, $headers); if (!$mailSent) { $errors['mailfail'] = true; } }
# END: require('processmail_05.php');
if ($mailSent) { header('Location: thank-you.php'); exit; } } // close: if (isset($_POST['send']))
$tools = true; include("{$_SERVER["DOCUMENT_ROOT"]}/php/includes/header.php"); ?> <main> <h2><?= $folder_name; ?> <span><?= $file_name; ?></span> </h2> <?php if ($_POST && $suspect) { ?> <p class="error">Sorry, your mail could not be sent. Please try later.</p> <?php } elseif ($missing || $errors) { ?> <p class="error">Please complete the missing item(s) indicated.</p> <?php } ?> <form id="feedback" method="post"> <fieldset> <legend><?= $file_name; ?></legend> <ol> <li<?php if ($missing && in_array('name', $missing)) echo ' class="error"'; ?>> <?php if ($missing && in_array('name', $missing)) { ?>
<strong>Please enter your name</strong> <?php } ?>