“未定义变量:”在php联络表单中提交表单后

时间:2019-05-29 06:34:20

标签: php html

我正面临php联系表单错误。每当我提交表单时,它就会显示错误消息“糟糕!出了点问题,我们无法发送您的消息。我已为内部服务器错误(500)设置了以下代码:

当我查看日志时

  

未定义变量:/../../ public_html / email.php中的email_content   第28行

<form action="email.php" method="post" class="mbr-form form-with-styler" data-form-title="Contact Form">
    <div class="dragArea row">
        <div class="col-md-6  form-group" data-for="name">
            <input type="text" name="name" placeholder="Your Name" data-form-field="Name" required="required" class="form-control input display-7" id="name-form4-d">
        </div>
        <div class="col-md-6  form-group" data-for="phone">
            <input type="text" name="phone" placeholder="Contact No." data-form-field="Phone" required="required" class="form-control input display-7" id="phone-form4-d">
        </div>
        <div data-for="email" class="col-md-12  form-group">
            <input type="text" name="email" placeholder="Email" data-form-field="Email" class="form-control input display-7" required="required" id="email-form4-d">
        </div>
        <div data-for="message" class="col-md-12  form-group">
            <textarea name="message" placeholder="Message" data-form-field="Message" class="form-control input display-7" id="message-form4-d"></textarea>
        </div>
        <div class="col-md-12 input-group-btn  mt-2 align-center">
            <button type="submit" class="btn btn-primary btn-form display-4">SEND MESSAGE</button>
        </div>
    </div>
</form>

email.php:

<?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);
        $phone = filter_var(trim($_POST["phone"]), FILTER_VALIDATE_INT);
        $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 !filter_var($phone, FILTER_VALIDATE_INT) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) ) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Please complete the form and try again.";
            exit;
        }

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

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

        // Build the email content.
        $email_content .= "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Contact No.: $phone\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! Your message has been sent.";
        } 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.";
    }

?>

相同类型的代码已在其他网站上成功运行。但是这里显示错误“糟糕!出问题了,我们无法发送您的消息。'

3 个答案:

答案 0 :(得分:0)

在这里,您将错误消息设置为单独的500,

       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.";

您可以轻松引发404错误消息,但是,这并不意味着找不到任何内容!您可以看到写入error.log的内容吗?邮件功能似乎不起作用!

答案 1 :(得分:0)

使用try catch获得确切错误。 由于邮件配置错误,很可能您正面临着这个问题

try {
 $success = mail($recipient, $subject, $email_content, $email_headers);
 if (!$success) {
     $errorMessage = error_get_last()['message'];
 }
} catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: $e";
}

OR

使用此

 print_r(error_get_last()); 

将帮助打印上一次发生的错误

答案 2 :(得分:0)

。=试图添加到尚不存在的变量,将代码替换为;-

// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Contact No.: $phone\n\n";
$email_content .= "Message:\n$message\n"