.php联系人表单正在提交但未发送

时间:2018-12-11 17:24:38

标签: php contact-form

我目前正在为我正在工作的网站整理一个非常简单的联系表单,但是由于某种原因,我的常规表单不起作用。 php如下:

    <?php
    // Check for HTML in comment
    function screen_form($ary_check_for_html)
    {
    // check array - reject if any content contains HTML.
    foreach($ary_check_for_html as $field_value)
    {        
    if(is_array($field_value))
    {
    foreach($field_value as $field_array)  // if the field value is an array, step through it
    {
    $stripped = strip_tags($field_array);
    if($field_array!=$stripped) 
    {
    // something in the field value was HTML
    return false;
    }
    }
    } else {
    $stripped = strip_tags($field_value);
    if($field_value!=$stripped) 
    {
    // something in the field value was HTML
    return false;
    }
    }
    }
    return true;
    }

    # Catcher for errors
    $FORM_ERROR = array();
    $SUCCESS = false;
    $resp = null;

    # Check for form processing
    if($_POST) {
    # Little bit of safety checking....
    # Added to try and halt spam....
    sleep(1);

    if(!$_POST['gonzo']) {
    die('No spam thank you.');
    }

    if(!screen_form($_POST)) {
    sleep(5);
    die('No HTML in forms thank you.');
    }

    $form_init = base64_decode($_POST['gonzo']);
    $now = time();
    $diff = $now - $form_init;

    if($form_init > $now || $diff > 1800) {
    die('Attempt failed - Please retry.');
    }

    $stamp = date('d-m-Y H:i:s', $form_init);

    # Validate submission....
    if(!$_POST['name']) {
    array_push($FORM_ERROR, 'name');
    }
    if(!$_POST['email']) {
    array_push($FORM_ERROR, 'email');
    }
    if(!$_POST['telephone']) {
    array_push($FORM_ERROR, 'telephone');
    }
    if(!$_POST['message']) {
    array_push($FORM_ERROR, 'message');
    }

    if(count($FORM_ERROR) < 1) {
    # Escape our values
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $telephone = htmlspecialchars($_POST['telephone']);
    $message = htmlspecialchars($_POST['message']);

    $msg = <<<EOF
    <h3>Contact Form Submission ($stamp)</h3>
    <table>
    <tr>
    <td><strong>Name</strong></td>
    <td>$name</td>
    </tr>
    <tr>
    <td><strong>Email</strong></td>
    <td>$email</td>
    </tr>
    <tr>
    <td><strong>Tel</strong></td>
    <td>$telephone</td>
    </tr>
    <tr>
    <td><strong>Message</strong></td>
    <td>$message</td>
    </tr>
    </table>
    EOF;
    $headers = "From: postmaster@DOMAIN.co.uk\r\n";
    $headers .= "Content-Type: text/html\r\n";

    # Sent email 
    mail("EMAIL", "EMAIL TITLE", $msg, $headers);

    $SUCCESS = true;
    }
    }
    ?>

该表单在应有的状态下可以正常工作并出错,并且还可以通过完整的方式告诉我表单已发送,但是没有发送至电子邮件地址或其他任何尝试?任何想法都将对此表示欢迎。这是表单代码:

    <form action="contact" method="post" id="contact_form">
            <?php
                if($SUCCESS) {
            ?>
            <div class="alert alert-success" role="alert">
                <p>Thank you, the form has been successfully submitted.</p>
                <p>We will be in touch as soon as possible with regards to your enquiry.</p>
            </div>
            <?php
                }
                else {
            ?>
            <p>Please Note: Fields Marked with a <span class="req">*</span> are required</p>
            <div class="form-group">
                <label form="name">Name <span class="req">*</span> <?php if(in_array('name', $FORM_ERROR)) print '<div class="req"><i class="fa fa-exclamation-triangle"></i> This field below is required!</div>'; ?></label>
                <input class="form-control" type="text" name="name" tabindex="1" placeholder="Joe">
            </div>

            <div class="form-group">
                <label form="email">Email <span class="req">*</span> <?php if(in_array('email', $FORM_ERROR)) print '<div class="req"><i class="fa fa-exclamation-triangle"></i> This field below is required!</div>'; ?></label>
                <input class="form-control" type="text" name="email" tabindex="2" placeholder="joe.bloggs@example.com">
            </div>

            <div class="form-group">
                <label form="telephone">Telephone <span class="req">*</span> <?php if(in_array('telephone', $FORM_ERROR)) print '<div class="req"><i class="fa fa-exclamation-triangle"></i> This field below is required!</div>'; ?></label>
                <input class="form-control" type="text" name="telephone" tabindex="3" placeholder="1234567890">
            </div>

            <div class="form-group">
                <label form="message">Message <span class="req">*</span> <?php if(in_array('message', $FORM_ERROR)) print '<div class="req"><i class="fa fa-exclamation-triangle"></i> This field below is required!</div>'; ?></label>
                <textarea class="form-control" name="message" tabindex="4" col="25" row="12" placeholder="I would like to discuss..."></textarea>
            </div>

            <div class="form-check">
                <p>By submitting this form you understand and agree to the <a href="terms">terms</a> set out on this website.</p>
            </div>

            <p>
                <input type="hidden" name="gonzo" value="<?php echo(base64_encode(time())); ?>">
                <button name="submit" type="submit">Submit Form</button>
            </p>
            <?php
                }
            ?>
        </form>

0 个答案:

没有答案