使用php,http response erorr发送电子邮件

时间:2016-01-11 11:59:32

标签: html email

我有问题使用php和jquery发送电子邮件,如果有人可以看看并帮助我,我会感谢他。 这是我的HTML

        <form id="contact" name="contact" method="post">
        <h2>
            <span>
                Read This FREE Report
            </span>
            Discover How You Could Become a Successful Forex Trader In As Little As 72 Hours From Now (or Less)!
        </h2>
        <input type="text" id="name" placeholder="Name" name="name" required />
        <input type="email" id="email" placeholder="Email" name="email" required />
        <button type="submit">
            Free Instant Access
        </button>
    </form>

Jquery的

        // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $('form').submit(function (event) {
        // Stop the browser from submitting the form.
        event.preventDefault();

        // Serialize the form data.
        var formData = $('form').serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: 'contact_form.php',
            data: formData
        })



          .done(function (response) {
              $(formMessages).text("Your form has been send..");

              // Set the message text.
              $(formMessages).text(response);

              // Clear the form.
              $('#name').val('');
              $('#email').val('');
          })
          .fail(function (data) {
              $(formMessages).text("Oops! An error occured and your message could not be sent");

              // Set the message text.
              if (data.responseText !== '') {
                  $(formMessages).text(data.responseText);
              } else {
                  $(formMessages).text('Oops! An error occured and your message could not be sent.');
              }
          });
    });

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);

    // Check that data was sent to the mailer.
    if ( empty($name)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! Dogodio se problem za vašim zahtjevom. Molimo pokušajte opet.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "email@hotmail.com";

    // Set the email subject.
    $subject = "Web contact from $name";

    // Build the email content.
    $email_content = "Person: $name\n";
    $email_content = "Email: $email\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
       http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Došlo je do pogreške, nismo uspjeli poslati vašu poruku.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "Dogodio se problem za vašim zahtjevom. Molimo pokušajte opet.";
}

&GT;

这是我得到的错误:
致命错误:在 /home2/speanut/public_html/freeforexreport.com/contact_form.php 35 < 中调用未定义的函数http_response_code() / b>

1 个答案:

答案 0 :(得分:0)

好像你的PHP版本低于5.4。 http_response_code()函数是在PHP 5.4中引入的。 我建议你更新PHP 5.4或更高版本。如果您不能这样做,可以使用以下兼容性代码:

// For 4.3.0 <= PHP <= 5.4.0
if (!function_exists('http_response_code'))
{
    function http_response_code($newcode = NULL)
    {
        static $code = 200;
        if($newcode !== NULL)
        {
            header('X-PHP-Response-Code: '.$newcode, true, $newcode);
            if(!headers_sent())
                $code = $newcode;
        }       
        return $code;
    }
}

有关更多参考,请查看以下question