Hello, I do not know why I do not receive the email via php, I have set preventDefault since I do not want it to be sent normally but by ajax, I receive the response through the alert BUT on I receive the message in my email. the status code is fine but not receaving. Any suggestions?
$('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();
$.post({
url:'http://www.franciscomanrique.com/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);
}
?>