2024-03-18 10:34:55 +01:00
|
|
|
<?php
|
|
|
|
require("/usr/share/php/libphp-phpmailer/autoload.php");
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
|
2025-04-01 10:32:54 +02:00
|
|
|
// Set header to return JSON
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
2024-03-18 10:34:55 +01:00
|
|
|
$mail = new PHPMailer(true);
|
|
|
|
|
2025-04-01 10:32:54 +02:00
|
|
|
$errors = [];
|
2024-03-18 10:34:55 +01:00
|
|
|
$myEmail = getenv('ASTRO_SMTP_FROM');
|
2024-03-18 10:35:43 +01:00
|
|
|
$myEmailSplitted = explode('@', $myEmail);
|
|
|
|
$domainFromMyEmail = (
|
|
|
|
empty($myEmailSplitted[1])
|
|
|
|
|| count($myEmailSplitted) != 2
|
|
|
|
) ? ''
|
|
|
|
: $myEmailSplitted[1];
|
|
|
|
|
2024-03-20 12:48:36 +01:00
|
|
|
$wantedContact = filter_input(INPUT_POST, 'contactTo', FILTER_SANITIZE_SPECIAL_CHARS);
|
2024-03-18 15:14:03 +01:00
|
|
|
$wantedContact = (
|
2024-03-18 10:35:43 +01:00
|
|
|
empty($wantedContact)
|
2024-03-20 12:48:36 +01:00
|
|
|
|| strpos($wantedContact, '@') !== false
|
|
|
|
|| strpos($wantedContact, '&') !== false
|
2024-03-18 10:35:43 +01:00
|
|
|
|| empty($domainFromMyEmail)
|
2024-03-18 15:14:03 +01:00
|
|
|
) ? $myEmail : "$wantedContact@$domainFromMyEmail" ;
|
2024-03-18 10:34:55 +01:00
|
|
|
|
2025-02-06 11:12:47 +01:00
|
|
|
/* hCaptcha */
|
|
|
|
$hcaptchaSecret = getenv('HCAPTCHA_SECRET_KEY');
|
|
|
|
$hcaptchaVerifyUrl = "https://api.hcaptcha.com/siteverify";
|
|
|
|
|
2024-03-18 10:34:55 +01:00
|
|
|
if(empty($_POST['namezzz']) || empty($_POST['emailzzz']) || empty($_POST['message'])) {
|
2025-04-01 10:32:54 +02:00
|
|
|
$errors[] = "Erreur : champs obligatoires manquants.";
|
2024-03-18 10:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($_POST['name']) && !empty($_POST['email'])) {
|
2025-04-01 10:32:54 +02:00
|
|
|
$errors[] = "Erreur : spam détecté.";
|
2025-02-06 11:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Captcha verification */
|
|
|
|
if(!empty($_POST['h-captcha-response'])) {
|
|
|
|
$responseKey = $_POST['h-captcha-response'];
|
|
|
|
$data = array(
|
|
|
|
'secret' => $hcaptchaSecret,
|
|
|
|
'response' => $responseKey
|
|
|
|
);
|
|
|
|
|
|
|
|
$checkRequest = curl_init();
|
|
|
|
curl_setopt($checkRequest, CURLOPT_URL, $hcaptchaVerifyUrl);
|
|
|
|
curl_setopt($checkRequest, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($checkRequest, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
|
|
curl_setopt($checkRequest, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$response = curl_exec($checkRequest);
|
|
|
|
curl_close($checkRequest);
|
|
|
|
|
|
|
|
$responseData = json_decode($response, true);
|
|
|
|
|
|
|
|
if(!$responseData['success']) {
|
2025-04-01 10:32:54 +02:00
|
|
|
$errors[] = "Erreur lors de la validation du captcha.";
|
2025-02-06 11:12:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
2025-04-01 10:32:54 +02:00
|
|
|
$errors[] = "Erreur lors de la validation du captcha.";
|
2024-03-18 10:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$name = $_POST['namezzz'];
|
|
|
|
$emailAddress = $_POST['emailzzz'];
|
|
|
|
$select = $_POST['select'];
|
|
|
|
$message = $_POST['message'];
|
|
|
|
$subscribe = $_POST['subscribe'];
|
|
|
|
|
|
|
|
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
|
2025-04-01 10:32:54 +02:00
|
|
|
$errors[] = "Erreur d'adresse e-mail invalide : $emailAddress";
|
2024-03-18 10:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($errors)) {
|
2025-04-01 10:32:54 +02:00
|
|
|
try {
|
|
|
|
$emailSubject = "[Formulaire Astrolabe] Nouveau message";
|
|
|
|
|
|
|
|
switch ($select) {
|
|
|
|
case "option 1":
|
|
|
|
$purpose = "Demande de rendez-vous";
|
|
|
|
break;
|
|
|
|
case "option 2":
|
|
|
|
$purpose = "Demande de précisions sur le statut d'entrepreneur salarié";
|
|
|
|
break;
|
|
|
|
case "option 3":
|
|
|
|
$purpose = "Proposition de misson";
|
|
|
|
break;
|
|
|
|
case "option 4":
|
|
|
|
$purpose = "Proposition de partenariat";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$purpose = "Autre demande";
|
|
|
|
}
|
|
|
|
$emailSubject .= " : $purpose";
|
|
|
|
|
|
|
|
$emailBody = "Vous avez reçu un nouveau message depuis le formulaire du site Astrolabe :".
|
|
|
|
"\r\n\r\nNom: $name \r\nEmail: $emailAddress \r\nRaison: $purpose\r\nSubscribe: $subscribe\r\n\r\n$message";
|
2024-03-18 10:34:55 +01:00
|
|
|
|
2025-04-01 10:32:54 +02:00
|
|
|
$emailBodyHTML = str_replace("\r\n", "<br>", $emailBody);
|
2024-03-18 10:34:55 +01:00
|
|
|
|
2025-04-01 10:32:54 +02:00
|
|
|
$mail->isSMTP();
|
|
|
|
$mail->Host = getenv('ASTRO_SMTP_HOSTNAME');
|
|
|
|
$mail->SMTPAuth = true;
|
|
|
|
$mail->Username = getenv('ASTRO_SMTP_USERNAME');
|
|
|
|
$mail->Password = getenv('ASTRO_SMTP_PASSWORD');
|
|
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
|
|
$mail->Port = 587;
|
|
|
|
|
|
|
|
//Options
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
$mail->WordWrap = 70;
|
|
|
|
|
|
|
|
//Recipients
|
|
|
|
$mail->setFrom($myEmail);
|
|
|
|
$mail->addAddress($wantedContact);
|
|
|
|
$mail->addReplyTo($emailAddress, $name);
|
|
|
|
|
|
|
|
// Content
|
|
|
|
$mail->isHTML(true);
|
|
|
|
$mail->Subject = $emailSubject;
|
|
|
|
$mail->Body = $emailBodyHTML;
|
|
|
|
$mail->AltBody = $emailBody;
|
|
|
|
|
|
|
|
$mail->send();
|
|
|
|
|
|
|
|
// if subscribe add to mailing list
|
|
|
|
if(!empty($subscribe)) {
|
|
|
|
// process
|
|
|
|
// enovoi mail add to mailing list
|
|
|
|
}
|
2024-03-18 10:34:55 +01:00
|
|
|
|
2025-04-01 10:32:54 +02:00
|
|
|
http_response_code(200);
|
|
|
|
echo json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'message' => 'Message envoyé avec succès'
|
|
|
|
]);
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
http_response_code(500);
|
|
|
|
echo json_encode([
|
|
|
|
'success' => false,
|
|
|
|
'errors' => ["Erreur lors de l'envoi du message : " . $mail->ErrorInfo]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode([
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $errors
|
|
|
|
]);
|
2024-03-18 10:34:55 +01:00
|
|
|
}
|