02 Mail Contact 08

02 Mail Contact 08

Contact 08

Debug Code





Debug Code



	
	






	

$_SERVER["SCRIPT_NAME"]
/php/assignments/02-mail/contact-08.php

print_r($_GET)
Array
(
)


print_r($_POST)
Array
(
)


print_r($_SESSION)
Array
(
)


print_r($_FILES)
Array
(
)



<?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($message70);
        
// 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 ?>

                    <label for="name">Name:</label>
                    <input name="name" id="name" type="text" class="formbox" <?php
                                                                                
if (isset($name)) {
                                                                                    echo 
' value="' htmlentities($name) . '"';
                                                                                } 
?>>
                    </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) . '"';
                                                                                    } 
?>>
                        </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);
                                                                                        } 
?></textarea>
                            </li>
                            <li<?php if (isset($errors['recaptcha'])) echo ' class="error"'?>>
                                <?php if (isset($errors['recaptcha'])) { ?>
                                    <strong>Incorrect Response</strong>
                                <?php }
                                echo 
"<label>Answer Challenge Question</label>";
                                echo 
g_recaptcha_get_form_control(); ?>
                                </li>
                                <li>
                                    <input name="send" id="send" type="submit" value="Send message">
                                </li>
            </ol>
        </fieldset>
    </form>


    <h2>Debug Code</h2>
    <pre class="line-numbers">
<code class="language-php">

<?php if ($_POST && $mailSent) {
    echo 
"Message body\n\n";
    echo 
htmlentities($messageENT_COMPAT'UTF-8') . "\n";
    echo 
"Headers\n\n";
    echo 
htmlentities($headersENT_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($expected)) {
    echo 
'$expected: ';
    
print_r($expected);
?>

<?php if (isset($name)) echo '$name: ' $name?>

<?php if (isset($email)) echo '$email: ' $email?>

<?php if (isset($comments)) echo '$comments: ' $comments?>

<?php if (isset($g_recaptcha_response->success)) echo '$g_recaptcha_response->success: ' $g_recaptcha_response->success?>

<?php if (isset($g_recaptcha_response)) echo 'var_dump($g_recaptcha_response): ' var_dump($g_recaptcha_response); ?>
    
</code>
</pre>

</main>
<?php
include("{$_SERVER["DOCUMENT_ROOT"]}/php/includes/sidebar.php");
include(
"{$_SERVER["DOCUMENT_ROOT"]}/php/includes/footer.php");
?>