Я использую следующий сценарий в качестве контактной формы на моем веб-сайте:
<?php
require('recaptcha-master/src/autoload.php');
$from = 'Contact Form <info@mydomain.com>';
$sendTo = 'Contact Form <contact@mydomain.com>';
$subject = 'New Contact';
$fields = array('name' => 'name', 'company' => 'company', 'email' => 'email', 'phone' => 'phone'); // array variable name => Text to appear in the email
$okMessage = 'Thank you, we will be in touch soon';
$errorMessage = 'There was an error while submitting the form. Please try again later';
$recaptchaSecret = 'xxxxxxxxxxx';
try
{
if (!empty($_POST)) {
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
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'];
}
Это очень хорошо работает для меня, однако я хотел бы добавить автоматическое спасибо, что вас отправят на указанный адрес электронной почты.адрес лица, отправившего форму.Есть ли способ сделать это в этом сценарии?Некоторые советы экспертов будут очень полезны.
Большое спасибо!