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.
<html>
<head>
<title>Contact Formulier</title>
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name">
<br>
Last Name: <input type="text" name="last_name">
<br>
Email: <input type="text" name="email">
<br>
Phone: <input type="text" name="phone">
<br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<br>
<div class="g-recaptcha" data-sitekey="MY SITEKEY-CODE"></div>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$to = "info@mail.com"; // 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 www.website.com";
$subject2 = "Copy of your message you have sent to www.website.com";
$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($to,$subject,$message,$headers);
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.
}
?>