PHP联系人表单失败


Failing PHP contact form

all。我正在用PHP创建一个联系人表单,但由于某种原因,我无法使其工作。消息未通过,并且不会显示"谢谢您的消息"的回复。我的PHP技术非常新手,所以任何帮助都将不胜感激!

<div id="form-div">
            <?php
                if($_SERVER['REQUEST_METHOD'] != 'POST') {
                    $self = $_SERVER['PHP_SELF'];
            ?>
            <form class="form" id="form1" method="post" name="contactForm" action=" <?php echo $self; ?> ">
                <p class="name">
                    <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
                </p>
                <p class="email">
                    <input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
                </p>
                <p class="text">
                    <textarea name="comment" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
                </p>
                <div class="submit">
                    <input type="submit" value="SEND" id="button-blue"/>
                </div>
            </form>
            <?php
                } else {
                    $name = $_POST['name'];
                    $emailFrom = $_POST['email'];
                    $comment = $_POST['comment'];
                    $emailTo = "address@email.com";
                    $subject = "Form for Website";
                    $header = "From: $name <$emailFrom>'r'nReply-To: $emailFrom'r'n";
                    $header .= "MIME-Version: 1.0'r'n";
                    $header .= "Content-type:text/html;charset=iso-8859'r'n";
                    $message = "<b>From:</b> $name<br><b>Email:</b> $emailFrom<br><b>Message:</b> <br>$comment";
                    mail($emailTo, $subject, $message, $header);
                    echo"Thank you for your message.";
                }
            ?>
        </div>

一般来说,我建议发送电子邮件的代码在任何html输出之前,尽管我不能100%确定它会出现在其他地方经常出现的可怕的headers already sent错误。您可能也希望考虑过滤POSTed变量,并使用strip_tags。然后,基于对mail的调用的返回值,向用户显示消息。。。。

<?php
    session_start();
    $res=false;
    /* The `PHP_SELF` server var is not considered secure */
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['email'],$_POST['name'] ) ){
        @ob_clean();

        $name = strip_tags( filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING ) );
        $emailFrom = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL ), FILTER_VALIDATE_EMAIL );
        $comment = strip_tags( filter_input( INPUT_POST, 'comment', FILTER_SANITIZE_STRING ) );
        $emailTo = "address@email.com";
        $subject = "Form for Website";
        $header = "From: $name <$emailFrom>'r'nReply-To: $emailFrom'r'n";
        $header .= "MIME-Version: 1.0'r'n";
        $header .= "Content-type:text/html;charset=iso-8859'r'n";
        $message = "<b>From:</b> $name<br><b>Email:</b> $emailFrom<br><b>Message:</b> <br>$comment";
        /* You do not want any HTML output before trying to send mail */
        $res=@mail( $emailTo, $subject, $message, $header );    
    }
?>
<html>
    <head>
        <title>Contact form</title>
    </head>
    <body><!-- I don't understand the syntax used in the class attributes of these form elements... -->
        <div id="form-div">
            <form class="form" id="form1" method="post" name="contactForm"><!-- omit the `action` tag to send, by default, to the same page -->
                <p class="name">
                    <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
                </p>
                <p class="email">
                    <input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
                </p>
                <p class="text">
                    <textarea name="comment" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
                </p>
                <div class="submit">
                    <input type="submit" value="SEND" id="button-blue"/>
                </div>
            </form>
            <?php
                if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['email'],$_POST['name'] ) ){
                    echo $res ? 'Thankyou for your mail' : 'Sorry, there was a problem';
                }
            ?>
        </div>
    </body>
</html>

检查服务器中是否关闭了php错误。在localhost中,如果配置不正确,发送邮件将失败。在这种情况下,进程在邮件发送代码(即mail($emailTo, $subject, $message, $header);)处退出,您的感谢消息将不会执行。检查一下。

参考:http://php.net/manual/en/function.error-reporting.php

更换怎么样

if($_SERVER['REQUEST_METHOD'] != 'POST') {

if(count($_POST) < 1) {