为什么我的PHP / HTML表单不向我发送数据

时间:2018-09-03 15:33:19

标签: php html

经过多年的不活动之后,我正在尝试PHP,我以为自己拥有了它,但看起来好像失去了联系。谁能看到为什么我可能无法将任何数据发送到我的电子邮件地址?

HTML代码:

<form name="Call Back Request" id="request-call-form" action="callbackrequest.php" method="POST" novalidate="novalidate">

    <div class="col-md-6">
        <input id="name" type="text" placeholder="Name" name="name">
        <input id="email" type="text" placeholder="Email" name="email">
    </div>

    <div class="col-md-6">
        <input id="phone" type="text" placeholder="Phone" name="phone">
        <input id="subject" type="text" placeholder="Subject" name="subject">
    </div>

    <div class="col-md-6">
        <button type="submit" value="submit" class="thm-btn"><a href="https://rootlayertechnologies.com.au/callbackrequest.php">submit now</a></button>
    </div>

    <div class="col-md-12">
        <div id="success"></div>
    </div>

</form>

PHP代码:

<?php
if (isset($_POST['email'])) {

    $email_to      = "xxxxx@xxxx.com";
    $email_subject = "Call Back Request Form - Home Page | rootlayertechnologies.com.au";

    function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error . "<br /><br />";
    echo "Please go back and fix these errors and submit the form again.<br /><br />";
    die();
    }

    // validation expected data exists
    if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['subject'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $telephone  = $_POST['phone']; // not required
    $comments   = $_POST['subject']; // required

    $error_message = "";
    $email_exp     = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if (!preg_match($email_exp, $email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if (!preg_match($string_exp, $name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }

    if (strlen($subject) < 2) {
    $error_message .= 'The Subject you entered does not appear to be valid.<br />';
    }

    if (strlen($error_message) > 0) {
    died($error_message);
    }

    $email_message = "Form details below.\n\n";

    function clean_string($string) {
    $bad = [
        "content-type",
        "bcc:",
        "to:",
        "cc:",
        "href"
    ];
    return str_replace($bad, "", $string);
    }

    $email_message .= "Name: " . clean_string($name) . "\n";
    $email_message .= "Email: " . clean_string($email_from) . "\n";
    $email_message .= "Phone: " . clean_string($phone) . "\n";
    $email_message .= "Subject: " . clean_string($subject) . "\n";

    // create email headers
    $headers = 'From: ' . $email_from . "\r\n"
    . 'Reply-To: ' . $email_from . "\r\n"
    . 'X-Mailer: PHP/' . phpversion();

    @mail($email_to, $email_subject, $email_message, $headers);
    ?>

        <!-- include your own success html here --> 
        <p>Thanks for contacting us. We have received your request for a call back and a friendly member of our Product Solutions team will be in touch with you soon.</p>

<?php}?>

1 个答案:

答案 0 :(得分:3)

我将从删除<a>按钮内的<submit>标签开始。我认为通过单击链接,您可能只是打开页面,而不是提交表单。

然后,在PHP代码上,我将确认$email_to的设置正确。 (这似乎很明显,但是始终值得确认。) 我无法测试代码,但没有发现任何错误本身。 (有些事情可以改善,例如缺少filter_var,也许缺少html_escapeaddslashesstrip_tags。)

这使我认为问题可能出在服务器级别。因此,这里有一些需要考虑的事情:

  • From: 必须是与该站点位于同一域的地址(例如info@rootlayertechnologies.com.au)。不过请保持Reply-to:不变。
  • 确保正确设置SPF记录。您可能还想研究DKIM和DMARC。
  • 一些托管公司禁用了PHP mail。您可能需要实现SMTP。