primer desarrollo
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
// LIMPIAR CUALQUIER SALIDA ANTES DE EMPEZAR
|
||||
if (ob_get_length() > 0) { ob_clean(); }
|
||||
|
||||
// DESACTIVAR TODOS LOS ERRORES (en producción)
|
||||
ini_set('display_errors', 0);
|
||||
ini_set('display_startup_errors', 0);
|
||||
error_reporting(0);
|
||||
|
||||
// CABECERAS PRIMERO
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
// FUNCIÓN PARA ENVIAR RESPUESTAS
|
||||
function sendResponse($status, $message, $code = 200) {
|
||||
http_response_code($code);
|
||||
echo json_encode([
|
||||
"status" => $status,
|
||||
"message" => $message
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// VERIFICAR MÉTODO
|
||||
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
||||
sendResponse("error", "Método no permitido", 405);
|
||||
}
|
||||
|
||||
// OBTENER DATOS
|
||||
$name = isset($_POST["name"]) ? strip_tags(trim($_POST["name"])) : '';
|
||||
$email = isset($_POST["email"]) ? filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL) : '';
|
||||
$phone = isset($_POST["phone"]) ? strip_tags(trim($_POST["phone"])) : '';
|
||||
$company = isset($_POST["company"]) ? strip_tags(trim($_POST["company"])) : '';
|
||||
$service = isset($_POST["service"]) ? strip_tags(trim($_POST["service"])) : '';
|
||||
$message_text = isset($_POST["message"]) ? strip_tags(trim($_POST["message"])) : '';
|
||||
|
||||
// VALIDAR
|
||||
if (empty($name) || empty($message_text) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
sendResponse("error", "Por favor completa todos los campos requeridos correctamente.", 400);
|
||||
}
|
||||
|
||||
// ENVIAR EMAIL (ya sabemos que esto funciona)
|
||||
$to = "info@vettaio.com";
|
||||
$subject = "Nuevo mensaje de contacto de $name - VettaIO";
|
||||
$email_content = "Nuevo mensaje de contacto:\n\n";
|
||||
$email_content .= "Nombre: $name\n";
|
||||
$email_content .= "Email: $email\n";
|
||||
$email_content .= "Teléfono: $phone\n";
|
||||
$email_content .= "Empresa: $company\n";
|
||||
$email_content .= "Servicio de interés: $service\n\n";
|
||||
$email_content .= "Mensaje:\n$message_text\n";
|
||||
|
||||
$email_headers = "From: $name <$email>\r\n";
|
||||
$email_headers .= "Reply-To: $email\r\n";
|
||||
|
||||
if (mail($to, $subject, $email_content, $email_headers)) {
|
||||
sendResponse("success", "¡Gracias! Tu mensaje ha sido enviado.", 200);
|
||||
} else {
|
||||
sendResponse("error", "Error al enviar el mensaje. Por favor, intenta nuevamente.", 500);
|
||||
}
|
||||
|
||||
// NO DEJAR NADA DESPUÉS DE ESTO - NI ESPACIOS, NI SALTOS DE LÍNEA
|
||||
?>
|
||||
Reference in New Issue
Block a user