基本的PHP回声问题

时间:2016-10-04 18:09:27

标签: php

PHP的新手,我尝试在其他stackoverflow线程以及在线上查找此内容,但似乎我一直在做正确的事情。我试图理解为什么我得到的200响应输出断字符(我看到文字br标签)。我只是想让你的询问等在新的一线上。

以下是代码:

<?php
    // Added input sanitizing to prevent injection
    // 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);
        $message = trim($_POST["message"]);
        // Check that data was sent to the mailer.
        if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
        }
        // Set the recipient email address.
        $recipient = "";
        // Set the email subject.
        $subject = "New Inquiry from $name";
        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Message:\n$message\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.<br>Your inquiry has been received, and we will get back to you as soon as possible.";
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message.";
        }
    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }
?>

1 个答案:

答案 0 :(得分:-1)

在这一行:

if (mail($recipient, $subject, $email_content, $email_headers)) {

您是否尝试将值$recipient? 它被定义为上面的空10行 我查看了你的代码,没有看到任何其他内容......

但这似乎是mail()失败的一个很好的理由 ;)