98 lines
3.8 KiB
PHP
98 lines
3.8 KiB
PHP
<?php
|
|
|
|
|
|
require_once __DIR__.'/PHPMailer-7.0.2/src/PHPMailer.php';
|
|
require_once __DIR__.'/PHPMailer-7.0.2/src/SMTP.php';
|
|
require_once __DIR__.'/PHPMailer-7.0.2/src/Exception.php';
|
|
require_once __DIR__.'/config.php';
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
if (!isset($_POST)) {
|
|
http_response_code(400);
|
|
echo "Erreur: Aucune donnée POST.";
|
|
exit;
|
|
}
|
|
|
|
$originUrl = $_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? null;
|
|
if($originUrl != null){
|
|
$origin = parse_url($originUrl, PHP_URL_HOST);
|
|
} else {
|
|
$origin = null;
|
|
}
|
|
|
|
if (in_array($origin, $allowed_origins)) {
|
|
header("Access-Control-Allow-Origin: " . $originUrl);
|
|
if (!isset($_POST['email']) || empty($_POST['email'])) {
|
|
http_response_code(400);
|
|
echo "Vous devez nous indiquez votre e-mail pour que nous puissions vous répondre.";
|
|
exit;
|
|
}
|
|
if (isset($_POST["website"]) && !empty($_POST["website"])) {
|
|
http_response_code(400);
|
|
echo "Il semblerait que vous avez rempli le formulaire automatiquement. Êtes-vous un humain ? " .
|
|
"En cas de problème lors de la soumission du formulaire, vous pouvez contactez directement la coopérative Champs Libres par e-mail.";
|
|
exit;
|
|
}
|
|
if (!isset($_POST["message"]) || empty($_POST["message"])) {
|
|
http_response_code(400);
|
|
echo "Message manquant ou vide";
|
|
exit;
|
|
}
|
|
//Create an instance; passing `true` enables exceptions
|
|
$mail = new PHPMailer(true);
|
|
|
|
try {
|
|
//Server settings
|
|
$mail->isSMTP(); //Send using SMTP
|
|
$mail->Host = $smtp_host; //Set the SMTP server to send through
|
|
$mail->SMTPAuth = true; //Enable SMTP authentication
|
|
$mail->Username = $smtp_user; //SMTP username
|
|
$mail->Password = $smtp_password; //SMTP password
|
|
if ($smtp_secure) {
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
|
|
}
|
|
$mail->Port = $smtp_port; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
|
|
|
|
//Recipients
|
|
$mail->setFrom($smtp_from, 'Chill website');
|
|
$mail->addAddress($smtp_to);
|
|
$mail->addReplyTo($_POST['email']);
|
|
|
|
//Content
|
|
$mail->isHTML(true); //Set email format to HTML
|
|
$mail->Subject = 'Message de chill.social : ' . $_POST['subject'];
|
|
|
|
$body = '';
|
|
foreach (['subject' => "Sujet", 'email' => 'E-mail', 'message' => 'Message'] as $k => $v) {
|
|
$body .= '<p><b>' . $v . '</b><br />' . nl2br($_POST[$k]) . '</p>';
|
|
}
|
|
$body .= '<p><b>Extra info : </b><pre>' . var_export([
|
|
"REMOTE_ADDR" => $_SERVER["REMOTE_ADDR"],
|
|
"HTTP_X_FORWARDED_FOR" => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : "not set",
|
|
"HTTP_CLIENT_IP" => isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : "not set",
|
|
"HTTP_USER_AGENT" => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "not set"
|
|
], $return = true) . '</pre>';
|
|
|
|
|
|
$mail->Body = $body;
|
|
$mail->send();
|
|
|
|
echo "Merci pour votre message.";
|
|
exit();
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo "Une erreur est survenue lors de l'envoie de votre message, veuillez ré-essayer. Si le problème persiste contactez-nous directement à info@champs-libres.coop";
|
|
if ($debug ?? false) {
|
|
echo "\nException : ". $e->getMessage();
|
|
}
|
|
exit();
|
|
}
|
|
|
|
} else {
|
|
http_response_code(400);
|
|
echo "Le message n'a pas pu être envoyé, veuillez ré-essayer (raison: Origine inconnue ou non autorisée).";
|
|
exit();
|
|
}
|
|
$mail = new PHPMailer(true);
|