I can not get the email sent, I get the confirmation alert by ajax but nothing is sent, status is 200 ok but it does not work
$('form').on('submit', function(e){
let nombre = $('#campoNombre').val();
let email = $('#campoEmail').val();
let telefono = $('#campoTelefono').val();
let mensaje = $('#campoMensaje').val();
if (nombre.length <= 2) {
alertaError('El nombre es demasiado corto');
e.preventDefault();
}
else if (email.includes('@') == false || email.includes('.') == false || email.indexOf('@') < 1) {
alertaError('El email no es correcto');
e.preventDefault();
}
else if (telefono.length < 9) {
alertaError('El telefono debe tener al menos 9 numeros');
e.preventDefault();
}
else if (mensaje.length <= 10) {
alertaError('El mensaje es demasiado corto');
e.preventDefault();
}
else{
e.preventDefault();
$.ajax({
type: "POST",
url: "contact.php",
data: $(this).serialize(),
success: function(datos) {
let texto = datos.saludo + '<br>' + datos.mensaje;
msg(texto, 2);
}
});
//Empty field info except button
$('input:not(.boton), textarea').each(function(){
$(this).val('');
});
}
});
});
//ERROR MSG
function alertaError(texto){
$('#alerta p').text(texto);
$('#alerta').addClass('visible');
setTimeout(function(){
$('#alerta').removeClass('visible');
},3000);
}
// MSG
function msg(mensajes, tiempo) {
$('#alerta p').html(mensajes);
$('#alerta').addClass('visible');
setTimeout( function(){
$('#alerta').removeClass('visible');
},tiempo * 2000);
}
<?php
header('Access-Control-Allow-Origin: *');
$jsondata = array();
$jsondata['saludo'] = '¡Hola '. $_POST['campoNombre'] . '!';
$jsondata['mensaje'] = 'Nos pondremos en contacto contigo lo antes posible';
header('Content-type: application/json; charset=utf-8');
echo json_encode($jsondata);
exit();
if($_POST) {
$to_Email = "[email protected]"; // email de recepción
$subject = 'SOLICITUD DE CONTACTO'; // asunto
// saneado de seguridad
$user_Name = filter_var($_POST["campoNombre"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["campoEmail"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["campoTelefono"], FILTER_SANITIZE_STRING);
// composición de email
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=charset=UTF-8";
$headers .= 'From: SOLICITUD DE CONTACTO' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = "Se ha recibido una nueva solicitud de contacto:"."\r\n";
$body .= "Nombre: " . $user_Name ."\r\n";
$body .= "Correo: " . $user_Email ."\r\n";
$body .= "Mensaje: " . $user_Message ."\r\n";
// envío de email
$sentMail = mail($to_Email, $subject, $body, $headers);
// if($sentMail)
// {
// header('Location: contact.php');
// //PROBAR METER AJAX CON JS sin mandar a pagina, con alerta en la misma pagina
// }
}
?>
<form action="contact.php" method="post" id="formulario">
<div class="row">
<div class="col-sm-4">
<input id="campoNombre" name="campoNombre" type="text" placeholder="Nombre...">
</div>
<div class="col-sm-4">
<input id="campoEmail" name="campoEmail" type="text" placeholder="Email...">
</div>
<div class="col-sm-4">
<input id="campoTelefono" name="campoTelefono" type="tel" placeholder="Telefono...">
</div>
<div class="col-xs-12">
<textarea id="campoMensaje" name="campoMensaje" type="text" placeholder="Mensaje..." rows="8" maxlength="999"></textarea>
</div>
<div class="col-xs-12">
<input type="submit" value="Enviar" class="boton">
</div>
</div>
</form>