2016-06-09 109 views
-1

我對PHP相當陌生。所以,這可能是我正在做的一個非常愚蠢的錯誤。我必須將表單詳細信息發送給郵件ID。我瀏覽互聯網,並獲得關於相同的各種鏈接。我收到了這些文件,並根據我的需要對其進行了更改。但是我面臨錯誤500,我無法理解這背後的原因。通過PHP將表單詳細信息提交給郵件

我的HTML是 -

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 

     <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame 
     Remove this if you use the .htaccess --> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 

     <title>app</title> 
     <meta name="description" content=""> 

     <meta name="viewport" content="width=device-width; initial-scale=1.0"> 

     <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references --> 
     <link rel="shortcut icon" href="/favicon.ico"> 
     <link rel="apple-touch-icon" href="/apple-touch-icon.png"> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
     <script src="app.js"></script> 
    </head> 

    <body> 
     <div id="form-messages" class="success"> 

     </div> 

     <form id="ajax-contact" method="post" action="mailer.php"> 
    <div class="field"> 
     <label for="name">Name:</label> 
     <input type="text" id="name" name="name" required> 
    </div> 

    <div class="field"> 
     <label for="email">Email:</label> 
     <input type="email" id="email" name="email" required> 
    </div> 

    <div class="field"> 
     <label for="message">Message:</label> 
     <textarea id="message" name="message" required> </textarea> 
    </div> 

    <div class="field"> 
     <button type="submit">Send</button> 
    </div> 
</form> 
    </body> 
</html> 

我mailer.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); 
     $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. 
     // FIXME: Update this to your desired email address. 
     $recipient = "[email protected]"; 

     // 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 .= "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."; 
    } 

?> 

我app.js文件是

$(function() { 

    // Get the form. 
    var form = $('#ajax-contact'); 

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

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

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

     // Submit the form using AJAX. 
     $.ajax({ 
      type: 'POST', 
      url: $(form).attr('action'), 
      data: formData 
     }).done(function(response) { 
      // Make sure that the formMessages div has the 'success' class. 
      $(formMessages).removeClass('error'); 
      $(formMessages).addClass('success'); 

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

      // Clear the form. 
      $('#name').val(''); 
      $('#email').val(''); 
      $('#message').val(''); 
     }).fail(function(data) { 
      // Make sure that the formMessages div has the 'error' class. 
      $(formMessages).removeClass('success'); 
      $(formMessages).addClass('error'); 

      // 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.'); 
      } 
     }); 

    }); 

}); 

任何人都可以請看看這個問題。我沒有太多,我無法解決這個問題。請幫幫我?

+0

你在你的php.ini文件中配置了郵件憑證嗎?或者你應該使用快捷郵件發送器是簡單的郵件發送功能。 –

+0

雅我配置了郵件ID] – shv22

回答

0

這是給500錯誤,因爲你在別的條件,這是越來越稱爲可能是因爲PHP mail()功能不可用在本地主機上(或在其上使用服務器)代碼

http_response_code(500); 

或者,您可以使用任何SMTP類發送電子郵件。另外,您可以使用SMTP類來配置您的gmail id。

-1

可能是你沒有設置郵件憑據在這裏看到你怎麼能做到這一點。 php manual

相關問題