php联系表单不起作用,错误消息&反弹回来

时间:2015-10-09 12:26:00

标签: php web contact

因此,当涉及到php表单时,我真的很糟糕,我收到消息"抱歉, 您的邮件无法发送...请检查您的电子邮件是否正确,否则字段丢失..."

其他人正在使用该网站说它也正在反弹。

HOW IT LOOKS

PHP

该文件夹不在php foler中,它与html页面一起出现。

<?php
//set your email here:
$yourEmail = 'emma@peacehavengc.com';
/*
 * CONTACT FORM
 */
//If the form is submitted
if(isset($_POST['submitted'])) { 
    //Check to make sure that the name field is not empty
    if($_POST['contact_name'] === '') { 
            $hasError = true;
    } else {
            $name = $_POST['contact_name'];
    }

    //Check to make sure sure that a valid email address is submitted
    if($_POST['emma@peacehavengc.com'] === '')  { 
            $hasError = true;
    } else if (!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $_POST['emma@peacehavengc.com'])) {
            $hasError = true;
    } else {
            $email = $_POST['emma@peacehavengc.com'];
    }

    //Check to make sure comments were entered  
    if($_POST['contact_textarea'] === '') {
            $hasError = true;
    } else {
            if(function_exists('stripslashes')) {
                    $comments = stripslashes($_POST['contact_textarea']);
            } else {
                    $comments = $_POST['contact_textarea'];
            }
    }

    //If there is no error, send the email
    if(!isset($hasError)) {

            $emailTo = $yourEmail;
            $subject = "Message From Your Website";
            $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
            $headers = 'From : my site <'.$emailTo.'>' . "\r\n" . 'answer to : ' . $email;

            mail($emailTo, $subject, $body, $headers);

            $emailSent = true; 
    }

}
?>

HTML

<div class="center">
    <h3>Get in touch Now</h3>
</div>
<form class="form-horizontal" method="post" action="contact.php" id="form">
    <div class="form-group">
        <label for="contact_name" class="col-lg-2 control-label">Name</label>
        <div class="col-lg-10">
            <input type="text" class="form-control" id="contact_name" name="contact_name">
        </div>
    </div>
    <div class="form-group">
        <label for="contact_email" class="col-lg-2 control-label">Email</label>
        <div class="col-lg-10">
            <input type="email" class="form-control" id="contact_email" name="contact_email">
        </div>
    </div>
    <div class="form-group">
        <label for="contact_textarea" class="col-lg-2 control-label">Message</label>
        <div class="col-lg-10">
            <textarea class="form-control" rows="3" id="contact_textarea" name="contact_textarea"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <input type="hidden" name="submitted" id="submitted" value="true" />
            <button type="submit" class="btn btn-default" name="submitted"><i class="icon-paperplane"></i>Send</button>
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

检查这个。

<?php
    $yourEmail = 'emma@peacehavengc.com';
    if(isset($_POST['submitted'])) 
    { 
        if($_POST['contact_name'] == '') 
        { 
                $hasError = true;
            } 
        else 
        {
                $name = $_POST['contact_name'];
            }

            //Check to make sure sure that a valid email address is submitted
            if($_POST['contact_email'] == '')  
        { 
                $hasError = true;
            } 
        else if (!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $_POST['contact_email'])) 
        {
                $hasError = true;
            } 
        else 
        {
                $email = $_POST['contact_email'];
            }

            //Check to make sure comments were entered  
            if($_POST['contact_textarea'] == '') 
        {
                $hasError = true;
            } 
        else 
        {
                if(function_exists('stripslashes')) 
            {
                        $comments = stripslashes($_POST['contact_textarea']);
                    } 
            else 
            {
                        $comments = $_POST['contact_textarea'];
                    }
            }

            //If there is no error, send the email
            if(!isset($hasError)) 
        {

                $emailTo = $yourEmail;
                $subject = "Message From Your Website";
                $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";            
        $headers  = "From: my site <$emailTo>" . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n";

                mail($emailTo, $subject, $body, $headers);

                $emailSent = true; 
            }
    header("location:contact.html");
    }
    ?>

this is what it says to do, delete the contact.html??