无法从此php表单获取电子邮件

时间:2014-02-18 02:30:33

标签: php html forms email

我正在我的网站上发送电子邮件表格。 出于某种原因,我无法通过此表单收到电子邮件。

HTML

        <form method="POST" action="processor.php" id="contactform" onsubmit="return(validateForm());">
        <div>
            <label for="fname">First Name:<br></label>
            <input type="text" name="fname" id="fname" value="" class="required"/>
        </div>
        <div>
            <label for="lname">Last Name:<br></label>
            <input type="text" name="lname" id="lname" value="" class="required"/>
        </div>
        <div>
            <label for="organization">Organization:<br></label>
            <input type="text" name="organization" id="organization" value="" class="required"/>
        </div>
         <div>
            <label for="email">Email:<br></label>
            <input type="text" name="email" id="email" value="" class="required email" />
        </div>

        <div>
            <label for="phone">Phone:<br></label>
            <input type="text" name="phone" id="phone" value="" class="required" />
        </div>          

        <div>
            <label for="message" style="line-height: 2;">Message:<br></label>
            <textarea rows="20" cols="20" name="message" id="message"></textarea>
        </div>

        <input id="submit" name="submit" type="submit" class="submit-button myButton" value="Send" />
        <div class="clearfix"></div>
    </form>

PHP

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 7 || !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
        die("Bad referer");
    $msg = "Website Message:\n\n\n";
    foreach ($_POST as $key => $val) {
        if (is_array($val)) {
            $msg .= "Item: $key\n\n";
            foreach ($val as $v) {
                $v = stripslashes($v);
                $msg .= " $v\n\n";
            }
        } else {
            $val = stripslashes($val);
            $msg .= "$key: $val\n\n";
        }
    }
    $recipient = "test@example.com";
    $subject   = "Website Contact Form Filled Out";
    error_reporting(0);
    if (mail($recipient, $subject, $msg)) {
        header("Location: http://www.example.com/thankyou.html");
    } else
        echo "An error occurred and the message could not be sent.";
} else
    echo "Bad request method";
?>

事情是PHP代码有效,因为它与其他HTML表单一起使用。 我想知道HTML编码的方式是否原因。 如果有人知道PHP并且形成良好,请帮助我。 谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

print_r($_POST);
exit;

在你的代码之前

// put it here
if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 7 || !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
            die("Bad referer");
    ....

如果您将看到包含变量的电子邮件 - 那么您的PHP脚本就会出现问题 并且 - 启用显示错误(即错误报告)

我看到了,你说它与另一种形式完美配合。我可以查看这份表格的代码吗?