0

So the PHP contact form for my website doesn't work. No idea why. Tried many different templates. Only one worked for a while, before I tried to insert captcha there, and then it also started making 500 error. No idea how PhP works from the word du tout - I just need a simple form for my personal website to have a captcha and that's all I ever want from PHP at this stage.

Below is the code that doesn't work (supposedly it should, as I have done it from this tutorial https://www.youtube.com/watch?v=4cr85kvM8I0

<?php

    if(isset($_POST['submit'])){

      $name = $_POST['name'];
      $subject = $_POST['subject'];
      $email_address = $_POST['email'];
      $message = $_POST['message'];

      $email_from = 'contact@mywebsite.com';
      $email_subject = 'New email that you have received';
      $email_body = "Name:  $name.\n".
                    "Subject:  $subject.\n".
                    "Email:  $email_address.\n".
                    "Message:  $message.\n";

      $to_email = "contact@mywebsite.com";
      $headers = "From: $email_from \r\n";
      $headers .= "Reply-to: $email_address\r\n";



      //your site secret key
      $secretKey = 'mykey';
      $responseKey = $_POST['g-captcha-response'];
      $UserIP = $_SERVER['REMOTE_ADDR'];
      $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";

      $response = file_get_contents($url);
            $response = json_decode($response);


      if ($response-> success)
      {
        mailto($to_email,$email_subject,$email_body,$headers);
        echo "Message Sent Successfully";
      }

      else {
        echo "Invalid Captcha, try again";
      }




}
?>

and this is the contact form (part of it on the html)

Head code:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>


...


      <div class="container">
        <h3>Contact Form</h3>
        <form action="contact-form-handler.php" method="POST"
        enctype="multipart/form-data" name="contact_form">
          <div class="nameemail">
            <label for="name">Name</label>
                <input name="name" type="text" required placeholder="Name"/>
            <label for="subject">Subject</label>
                <input name="subject" type="text" required placeholder="Subject"/>
            <label for="email">Email</label>
            <input name="email" type="email" required placeholder="you@domain.com"/>
          </div>
          <br>
            <label for="message">Message</label><br>
            <textarea name="message" cols="30" rows="10" placeholder="Enter your message here ..." required> </textarea>
            <div class="center">
            <div class="g-recaptcha" data-sitekey="mykey"></div>
                <input type="submit" value="Submit">
            </div>
        </form>
      </div>

So why in the mother of God this doesn't work?

  • 2
    `mailto` is not php function. Its [mail](https://www.php.net/manual/en/function.mail.php). I assume that you have not set php to display errors, So when a fatal error occured due to `mailto` its reported in error log but 500 http code is sent by server to indicate fatal error. [Display php errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) or check error log. – Umair Khan Aug 12 '20 at 11:28
  • 1
    500 Internal Server Error is a generic error message informing you that the server crashed while processing the request. Beyond that, it's meaningless, and is of very little use for debugging. You need to check the error logs on the server to try and find the underlying exception message. Once you've got that, you stand a chance of identifying the problem. As Umair says, the incorrect "mailto" is definitely a problem, but that's not to say there aren't also other errors. – ADyson Aug 12 '20 at 11:32
  • P.S. This guide shows you how to set up error logging / reporting with PHP: https://stackify.com/php-error-logs-guide/ . Use on-screen reporting in your dev/test environment for ease of use, and use logging in the live environment so you don't expose sensitive error messages to the outside world. – ADyson Aug 12 '20 at 11:32

0 Answers0