website/assets/php/send_mail.php

90 lines
2.7 KiB
PHP
Raw Normal View History

2026-03-16 10:30:48 +01:00
<?php
/**
2026-03-16 16:29:29 +01:00
* OmégaKube — Envoi formulaire de contact
2026-03-16 10:30:48 +01:00
*/
header('Content-Type: application/json; charset=utf-8');
2026-03-16 16:29:29 +01:00
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
2026-03-16 10:30:48 +01:00
header('Access-Control-Allow-Headers: Content-Type');
2026-03-16 16:29:29 +01:00
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { exit; }
2026-03-16 10:30:48 +01:00
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
2026-03-16 16:29:29 +01:00
die(json_encode(['success' => false, 'error' => 'POST requis']));
2026-03-16 10:30:48 +01:00
}
2026-03-16 16:29:29 +01:00
// === CONFIG ===
$to = 'contact@omegakube.fr';
2026-03-16 10:30:48 +01:00
2026-03-16 16:29:29 +01:00
// === LECTURE ===
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if (empty($data)) $data = $_POST;
if (empty($data)) {
die(json_encode(['success' => false, 'error' => 'Aucune donnée reçue']));
2026-03-16 10:30:48 +01:00
}
2026-03-16 16:29:29 +01:00
foreach ($data as $k => $v) {
$data[$k] = trim(strip_tags((string)$v));
}
2026-03-16 10:30:48 +01:00
2026-03-16 16:29:29 +01:00
// === VALIDATION ===
2026-03-16 10:30:48 +01:00
$errors = [];
2026-03-16 16:29:29 +01:00
if (empty($data['name']) || mb_strlen($data['name']) < 2) $errors[] = 'Nom invalide';
if (empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) $errors[] = 'Email invalide';
if (empty($data['message']) || mb_strlen($data['message']) < 10) $errors[] = 'Message trop court';
2026-03-16 10:30:48 +01:00
2026-03-16 16:29:29 +01:00
if ($errors) {
die(json_encode(['success' => false, 'errors' => $errors]));
2026-03-16 10:30:48 +01:00
}
2026-03-16 16:29:29 +01:00
// === HONEYPOT ===
2026-03-16 10:30:48 +01:00
if (!empty($data['website'])) {
2026-03-16 16:29:29 +01:00
die(json_encode(['success' => true, 'message' => 'Message envoyé !']));
2026-03-16 10:30:48 +01:00
}
2026-03-16 16:29:29 +01:00
// === SUJET ===
$subject = 'Omegakube form - ' . $data['name'] . ' - ' . date('H:i:s');
// === CORPS ===
$sep = str_repeat('=', 40);
$body = "OmegaKube — Nouveau message de contact\n";
$body .= $sep . "\n\n";
$body .= "Nom : " . $data['name'] . "\n";
$body .= "Email : " . $data['email'] . "\n";
$body .= "Objet : " . ($data['subject'] ?? 'Non précisé') . "\n";
2026-03-16 16:29:29 +01:00
$body .= "\n" . $sep . "\n\n";
$body .= "MESSAGE :\n\n";
$body .= $data['message'] . "\n\n";
$body .= $sep . "\n\n";
$body .= "Répondre : " . $data['email'] . "\n";
$body .= "Reçu le : " . date('d/m/Y H:i') . "\n";
2026-03-16 16:29:29 +01:00
$body .= "IP : " . ($_SERVER['REMOTE_ADDR'] ?? '') . "\n\n";
$body .= $sep . "\n";
$body .= "OmegaKube — omegakube.fr\n";
// === HEADERS ===
$headers = implode("\r\n", [
'MIME-Version: 1.0',
'Content-Type: text/plain; charset=UTF-8',
'From: OmegaKube <noreply@omegakube.fr>',
'Reply-To: ' . $data['email']
]);
// === ENVOI ===
$sent = mail($to, $subject, $body, $headers);
// === REPONSE ===
2026-03-16 10:30:48 +01:00
if ($sent) {
2026-03-16 16:29:29 +01:00
echo json_encode([
'success' => true,
'message' => 'Votre message a bien été envoyé ! Nous vous répondrons rapidement.'
2026-03-16 16:29:29 +01:00
]);
2026-03-16 10:30:48 +01:00
} else {
http_response_code(500);
2026-03-16 16:29:29 +01:00
echo json_encode([
'success' => false,
'error' => 'Erreur lors de l\'envoi. Contactez-nous directement sur Discord.'
]);
}