php – verifying Google reCAPTCHA v2-ThrowExceptions

Exception or error:

I’m trying to develop a contact-form for my wifes website.

I found this code that works very well and does what it needs to do (Send a copy to the sender of the message)

The only thing I want to ad is a google Recaptcha v2.
I’m not really a programmer, but i’m trying to cut paste some code together…. so bear me.

the code i’m using, I found also on Stackoverflow

Send email with PHP from html form on submit with the same script

I already added the recaptcha into the html-part.
But I can’t manage it to have the php part to handle it.

<title>Contact Formulier</title>
<script src=""></script>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name">
Last Name: <input type="text" name="last_name">
Email: <input type="text" name="email">
Phone: <input type="text" name="phone">
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<div class="g-recaptcha" data-sitekey="MY SITEKEY-CODE"></div>
<input type="submit" name="submit" value="Submit">


    $to = ""; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $phone = $_POST['phone'];
    $subject = "Mailform from";

    $subject2 = "Copy of your message you have sent to";

    $message = $first_name . " " . $last_name . "\n" . $from . "\n". $phone . "\n" ." wrote  this:" . "\n\n" . $_POST['message'];
    $message2 = $first_name . ", copy of your message : " ."\n\n" . $_POST['message'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    // You cannot use header and echo together. It's one or the other.
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *