I'm not great at PHP. I saw another post here asking the same thing, but the code is different and I tried a few things to see if it would work, but I'm not sure what all I need to change to make it function correctly.
I would like for the customer's email address to be in the send to when you hit reply. Right now, when you hit reply, it fills it in with the contact form email address.
{
<?php
// Require ReCaptcha class
require('recaptcha-master/src/autoload.php');
// Configure
// an email address that will be in the From field of the email.
$from = 'Contact form <[email protected]>';
// An email address that will receive the email with the output of the form
$sendTo = 'Contact form <[email protected]>';
// Subject of the email
$subject = 'New message from Customer';
// Array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message', 'tel' => 'Telephone Number' , 'add' => 'Address' , 'comp' => 'Company Name', 'product' => 'Product');
// Message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon! <br> You\'ll be redirected in about 5 secs. If not, click <a href="http://www.rs-cryo-equipment.com/contact-us.php">Here.</a>.';
header("refresh:5;url=http://www.back to/contact-us.php");
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
// ReCaptch Secret
$recaptchaSecret = 'SSHHHHH';
// Let's do the sending
// If you are not debugging and don't need error reporting,
// turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try {
if (!empty($_POST)) {
// Validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
// Do not forget to enter your secret key
// from https://www.google.com/recaptcha/admin
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// We validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
// Everything went well, and we can compose the message, as usual
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
} catch (\Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
}
To modify your PHP code so that the customer's email becomes the
Reply-Toaddress in the contact form, you would need to, first, extract the email from the POST data. Assuming the form field for the email is named 'email':Then, modify the headers to include the 'Reply-To' with the customer's email, as seen in [PHP mail() function][https://www.php.net/manual/en/function.mb-send-mail.php].
Note: you might want to validate or sanitize the email before using it.
See for instance "How to sanitize user input in PHP before mailing?" or "PHP email validation - filter validate and filter sanitize"
Your script would be: