HTML Form
Check For Empty Control Values
Check For Valid Email
Check For Suspect Strings
Controls Are Sticky
Debug: Post, Errors, Missing
Debug: Email: Message Body, Email: Headers
<?php # Email Validation and Sending $errors = []; $missing = []; // check if the form has been submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// email processing script $to = 'robert@nwtc-web.com'; $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<feedback@example.com>'; $headers[] = 'Content-Type: text/plain; charset=utf-8';
# 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 = true; }