Connect site to DB and enable contact email
This commit is contained in:
+104
-80
@@ -1,80 +1,104 @@
|
||||
<?php
|
||||
|
||||
// configure
|
||||
$from = 'info@yourdomain.com'; // Replace it with Your Hosting Admin email. REQUIRED!
|
||||
$sendTo = 'your@mail.com'; // Replace it with Your email. REQUIRED!
|
||||
$subject = 'New message from contact form';
|
||||
$fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message'); // array variable name => Text to appear in the email. If you added or deleted a field in the contact form, edit this array.
|
||||
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
|
||||
$errorMessage = 'There was an error while submitting the form. Please try again later';
|
||||
|
||||
// let's do the sending
|
||||
|
||||
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
|
||||
//your site secret key
|
||||
$secret = '6LdqmCAUAAAAANONcPUkgVpTSGGqm60cabVMVaON';
|
||||
//get verify response data
|
||||
|
||||
$c = curl_init('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
|
||||
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
|
||||
$verifyResponse = curl_exec($c);
|
||||
|
||||
$responseData = json_decode($verifyResponse);
|
||||
if($responseData->success):
|
||||
|
||||
try
|
||||
{
|
||||
$emailText = nl2br("You have new message from Contact Form\n");
|
||||
|
||||
foreach ($_POST as $key => $value) {
|
||||
|
||||
if (isset($fields[$key])) {
|
||||
$emailText .= nl2br("$fields[$key]: $value\n");
|
||||
}
|
||||
}
|
||||
|
||||
$headers = array('Content-Type: text/html; 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'];
|
||||
}
|
||||
|
||||
else:
|
||||
$errorMessage = 'Robot verification failed, please try again.';
|
||||
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
|
||||
$encoded = json_encode($responseArray);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
echo $encoded;
|
||||
endif;
|
||||
else:
|
||||
$errorMessage = 'Please click on the reCAPTCHA box.';
|
||||
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
|
||||
$encoded = json_encode($responseArray);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
echo $encoded;
|
||||
endif;
|
||||
|
||||
<?php
|
||||
require __DIR__ . '/../config/db.php';
|
||||
|
||||
$profileId = 1;
|
||||
|
||||
$contactMetaStmt = $pdo->prepare('SELECT email_to, email_from, recaptcha_secret FROM contact_meta WHERE profile_id = ? AND is_active = 1 LIMIT 1');
|
||||
$contactMetaStmt->execute([$profileId]);
|
||||
$contactMeta = $contactMetaStmt->fetch();
|
||||
|
||||
$emailTo = $contactMeta['email_to'] ?? 'mgonzalez@linkedcorp.net';
|
||||
$emailFrom = $contactMeta['email_from'] ?? $emailTo;
|
||||
$recaptchaSecret = $contactMeta['recaptcha_secret'] ?? '';
|
||||
|
||||
$subject = 'New message from contact form';
|
||||
$fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
|
||||
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
|
||||
$errorMessage = 'There was an error while submitting the form. Please try again later';
|
||||
|
||||
function json_response($type, $message)
|
||||
{
|
||||
$responseArray = array('type' => $type, 'message' => $message);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($responseArray);
|
||||
exit;
|
||||
}
|
||||
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$userSubject = trim($_POST['subject'] ?? '');
|
||||
$message = trim($_POST['message'] ?? '');
|
||||
|
||||
if ($name === '' || $email === '' || $userSubject === '' || $message === '') {
|
||||
json_response('danger', 'All fields are required.');
|
||||
}
|
||||
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
json_response('danger', 'Valid email is required.');
|
||||
}
|
||||
|
||||
$insertStmt = $pdo->prepare('INSERT INTO contact_messages (profile_id, full_name, email, subject, message, send_status) VALUES (?, ?, ?, ?, ?, ?)');
|
||||
$insertStmt->execute([$profileId, $name, $email, $userSubject, $message, 'pending']);
|
||||
$messageId = (int) $pdo->lastInsertId();
|
||||
|
||||
$sendStatus = 'failed';
|
||||
$sendError = null;
|
||||
$sentAt = null;
|
||||
|
||||
if ($recaptchaSecret !== '') {
|
||||
$recaptchaResponse = $_POST['g-recaptcha-response'] ?? '';
|
||||
if ($recaptchaResponse === '') {
|
||||
$sendError = 'recaptcha_missing';
|
||||
$updateStmt = $pdo->prepare('UPDATE contact_messages SET send_status = ?, send_error = ? WHERE id = ?');
|
||||
$updateStmt->execute([$sendStatus, $sendError, $messageId]);
|
||||
json_response('danger', 'Please click on the reCAPTCHA box.');
|
||||
}
|
||||
|
||||
$c = curl_init('https://www.google.com/recaptcha/api/siteverify?secret=' . $recaptchaSecret . '&response=' . $recaptchaResponse);
|
||||
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
|
||||
$verifyResponse = curl_exec($c);
|
||||
$responseData = json_decode($verifyResponse);
|
||||
if (!$responseData || empty($responseData->success)) {
|
||||
$sendError = 'recaptcha_failed';
|
||||
$updateStmt = $pdo->prepare('UPDATE contact_messages SET send_status = ?, send_error = ? WHERE id = ?');
|
||||
$updateStmt->execute([$sendStatus, $sendError, $messageId]);
|
||||
json_response('danger', 'Robot verification failed, please try again.');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$emailText = nl2br("You have new message from Contact Form\n");
|
||||
|
||||
foreach (['name' => $name, 'email' => $email, 'subject' => $userSubject, 'message' => $message] as $key => $value) {
|
||||
if (isset($fields[$key])) {
|
||||
$emailText .= nl2br($fields[$key] . ': ' . $value . "\n");
|
||||
}
|
||||
}
|
||||
|
||||
$headers = array(
|
||||
'Content-Type: text/html; charset="UTF-8"',
|
||||
'From: ' . $emailFrom,
|
||||
'Reply-To: ' . $email,
|
||||
'Return-Path: ' . $emailFrom,
|
||||
);
|
||||
|
||||
$sent = mail($emailTo, $subject, $emailText, implode("\n", $headers));
|
||||
|
||||
if ($sent) {
|
||||
$sendStatus = 'sent';
|
||||
$sentAt = date('Y-m-d H:i:s');
|
||||
} else {
|
||||
$sendError = 'mail_failed';
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$sendError = $e->getMessage();
|
||||
}
|
||||
|
||||
$updateStmt = $pdo->prepare('UPDATE contact_messages SET send_status = ?, send_error = ?, sent_at = ? WHERE id = ?');
|
||||
$updateStmt->execute([$sendStatus, $sendError, $sentAt, $messageId]);
|
||||
|
||||
if ($sendStatus === 'sent') {
|
||||
json_response('success', $okMessage);
|
||||
}
|
||||
|
||||
json_response('danger', $errorMessage);
|
||||
|
||||
Reference in New Issue
Block a user