Last active
November 25, 2017 22:04
-
-
Save juelvaldivia/6b9411d10226d4be16293a760fa9b29e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(isset($_POST['email'])) { | |
// Debes editar las próximas dos líneas de código de acuerdo con tus preferencias | |
$email_to = "[email protected]"; | |
$email_subject = "Contacto desde el sitio web"; | |
// Aquí se deberían validar los datos ingresados por el usuario | |
if(!isset($_POST['name']) || !isset($_POST['message']) || !isset($_POST['email'])) | |
{ | |
echo "Es necesario escribir todos los campos."; | |
die(); | |
} | |
$name = $_POST['name']; | |
$message = $_POST['message']; | |
$email = $_POST['email']; | |
if($name == "" || $message == "" || $email == ""){ | |
echo "Es necesario escribir todos los campos."; | |
die(); | |
} | |
if(!comprobar_email($email)){ | |
echo '¡El email introducido NO es correcto!'; | |
die(); | |
} | |
$email_message = "Detalles del formulario de contacto:\n\n"; | |
$email_message .= "Nombre: " . $_POST['name'] . "\n"; | |
$email_message .= "E-mail: " . $_POST['email'] . "\n"; | |
$email_message .= "Comentarios: " . $_POST['message'] . "\n\n"; | |
$email_from = "[email protected]"; | |
// Ahora se envía el e-mail usando la función mail() de PHP | |
$headers = 'From: '.$email_from."\r\n". | |
'Reply-To: '.$email_from."\r\n" . | |
'X-Mailer: PHP/' . phpversion(); | |
try{ | |
@mail($email_to, $email_subject, $email_message, $headers); | |
echo "1"; | |
}catch(Exception $ex){ | |
echo "".$ex; | |
} | |
}else{ | |
echo "Es necesario ingresar el correo"; | |
} | |
function comprobar_email($email) { | |
return (filter_var($email, FILTER_VALIDATE_EMAIL)) ? 1 : 0; | |
} | |
?> | |
// HTML | |
<form id="frmSendMail" class=""> | |
<input type="text" name="name" value="" placeholder="Nombre"> | |
<input type="email" name="email" value="" placeholder="Correo electronico"> | |
<textarea name="message" placeholder="Mensaje" rows="8" cols="80"></textarea> | |
<input type="submit" value="ENVIAR" /> | |
</form> | |
// javascript | |
$("#frmSendMail").submit(function(e){ | |
e.preventDefault(); | |
const params = $(this).serialize(); | |
const url = "mail.php" | |
$.post(url, params, function (data) { | |
if(data == "1"){ | |
alert("Se enviaron los comentarios satisfactoriamente") | |
}else{ | |
alert(data) | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment