为什么网站联系表格不起作用?

时间:2011-11-02 15:04:37

标签: php forms email

我写了以下联系表格。但它是或正在工作。请帮帮我......

<?php
    if (isset($_POST['submit'])) {
        $ToEmail = 'mymail@gmail.com';
        $EmailSubject = 'Site contact form ';
        $mailheader = "From: " . $_POST["email"] . "\r\n";
        $mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
        $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $MESSAGE_BODY = "Name: " . $_POST["name"] . "<br>";
        $MESSAGE_BODY .= "Email: " . $_POST["email"] . "<br>";
        $MESSAGE_BODY .= "Comment: " . nl2br($_POST["comment"]) . "<br>";
        mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) 
        or die("Failure");

        echo "<p>Your message was sent.</p>"; 
    }         
?>

<form action="index.php?page=contacts" method="post">                
    <p>Your name:<br/>
        <input name="name" type="text"  size="32">
    </p>
    <p>Email address:<br/>
        <input name="email" type="text"  size="32">
    </p>
    <p>Message:<br/>
        <textarea name="comment" cols="55" rows="8"></textarea>
    </p>
    <p>
        <input type="submit" name="Submit" value="Send">
    </p>
</form>

如果您发现任何错误,请告诉我。提前谢谢。

3 个答案:

答案 0 :(得分:2)

如果mail()返回false,那么您的PHP配置不正确。如果您使用的是Windows主机,则需要在php.ini中定义有效的SMTPsmtp_port设置,并且SMTP服务器必须已启动并运行。

那,或者你传递了“To:”的错误值或者SMTP服务器完全拒绝的其他标题。

答案 1 :(得分:0)

您的提交按钮名称为“提交”,但您正在检查“提交”。在处理Get / Post变量时,大小写很敏感。

除此之外看起来它会起作用。虽然,在通过电子邮件发送数据之前清理数据是个好主意。这样的事情会更好:

<?php
            if (isset($_POST['Submit'])) {
                $ToEmail = 'sustcorpus@gmail.com';
                $EmailSubject = 'Site contact form ';
                $mailheader = "From: " . htmlentities(strip_tags(trim($_POST["email"]))) . "\r\n";
                $mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
                $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
                $MESSAGE_BODY = "Name: " . htmlentities(strip_tags(trim($_POST["name"]))) . "<br>";
                $MESSAGE_BODY .= "Email: " . htmlentities(strip_tags(trim($_POST["email"]))) . "<br>";
                $MESSAGE_BODY .= "Comment: " . nl2br(htmlentities(strip_tags(trim($_POST["comment"])))) . "<br>";
                mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) 
                or die("Failure");

            echo "<p>Your message was sent.</p>"; 
        }

        ?>
            <form action="index.php?page=contacts" method="post">

                        <p>Your name:<br/>
                        <input name="name" type="text" value="<?php echo $_POST['name']?>"  size="32">
                        </p>
                        <p>Email address:<br/>
                        <input name="email" type="text" value="<?php echo $_POST['email']?>"  size="32">
                        </p>
                        <p>Message:<br/>
                        <textarea name="comment" cols="55" rows="8"><?php echo $_POST['comment']?></textarea>
                        </p>
                        <p>
                            <input type="submit" name="Submit" value="Send">
                        </p>
            </form>

答案 2 :(得分:0)

Php区分大小写,更改

if (isset($_POST['submit'])) {

使用:

if (isset($_POST['Submit'])) {

因为在您的表单中,您已经写了提交按钮的名称:提交