Contact Us
Contact Info:
- 612.432.8539
3649 Point Lane
Green Bay, Wisconsin 54311
3649 Point Lane
Green Bay, Wisconsin 54311
<?php
ini_set("date.timezone", "America/Chicago");
# Page specific variables.
$nav = "assignments";
$title_section = "03 Mail";
$tools = true; // Robert's Custom Variable (Do Not Use)
# 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 = 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 ($_SERVER['REQUEST_METHOD'] == 'POST'))
## include functions
include("../../includes/functions/general.php"); // Robert's Custom Function (Do Not Use)
# Create a human friendly name based on file name.
include("../../includes/title-page-name.php");
# The header section of the layout.
include("../../includes/header.php");
?>
<main>
<h2>
<?php echo $title_section; ?>
<span><?php echo $title_page_name; ?></span>
</h2>
<h2>
HTML Form
<br>Check For Empty Control Values
<br>Check For Valid Email
<br>Check For Suspect Strings
<br>Controls Are Sticky
<br>Debug: Post, Errors, Missing
<br>Debug: Email: Message Body, Email: Headers
</h2>
<?php if (($_POST && $suspect) || ($_POST && isset($errors['mailfail']))) { ?>
<p class="error">Sorry, your mail could not be sent.
Please try later.</p>
<?php } elseif ($missing || $errors) { ?>
<p class="error">Please fix the item(s) indicated.</p>
<?php } ?>
<form id="feedback" method="post">
<fieldset>
<legend><?php echo $title_page_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 } ?>
<label for="name">Name:</label>
<input name="name" id="name" type="text" class="formbox"
<?php if (isset($name)) {
echo 'value="' . htmlentities($name, ENT_COMPAT, 'UTF-8') . '"';
} ?>>
</li>
<li<?php if ( ($missing && in_array('email', $missing)) || (isset($errors['email'])) ) echo ' class="error"'; ?>>
<?php if ($missing && in_array('email', $missing)) { ?>
<strong>Please enter your email</strong>
<?php } elseif (isset($errors['email'])) { ?>
<strong>Invalid email address</strong>
<?php } ?>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="formbox"
<?php if (isset($email)) {
echo 'value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '"';
} ?>>
</li>
<li<?php if ($missing && in_array('comments', $missing)) echo ' class="error"'; ?>>
<?php if ($missing && in_array('comments', $missing)) { ?>
<strong>Please enter your comments</strong>
<?php } ?>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="60" rows="8"><?php
if (isset($comments)) {
echo htmlentities($comments, ENT_COMPAT, 'UTF-8');
//echo $comments; //</textarea><h1>HELLO WORLD</h1><style>aside{display:none}</style><textarea>
} ?></textarea>
</li>
<li>
<input name="send" id="send" type="submit" value="Send message">
</li>
</ol>
</fieldset>
</form>
<h2>Debug Code - Email: Message Body - Headers</h2>
<pre class="line-numbers">
<code class="language-php">
<?php
if ($_POST && $mailSent) {
echo "Email - Message Body:\n\n";
echo htmlentities($message, ENT_COMPAT, 'UTF-8') . "\n";
echo "Email - Headers:\n\n";
echo htmlentities($headers, ENT_COMPAT, 'UTF-8');
}
?>
</code>
</pre>
<h2>Debug Code</h2>
<pre class="line-numbers">
<code class="language-php">
<?php if($_POST) { echo '$_POST: '; print_r($_POST); } ?>
<?php if($errors) { echo '$errors: '; print_r($errors);} ?>
<?php if($missing) { echo '$missing: '; print_r($missing);} ?>
<?php if(isset($name)) echo '$name: ' . $name; ?>
<?php if(isset($email)) echo '$email: ' . $email; ?>
<?php if(isset($comments)) echo '$comments: ' . $comments; ?>
</code>
</pre>
</main>
<?php
include("../../includes/sidebar-dirs.php");
include("../../includes/footer.php");
?>