Last active
January 7, 2016 17:24
-
-
Save jorgechavz/2a1e6c338b692f5d331d to your computer and use it in GitHub Desktop.
Contact mail System
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 | |
require_once '../../includes/mandrill/Mandrill.php'; | |
header('Cache-Control: no-cache, must-revalidate'); | |
header('Content-type: application/json'); | |
extract($_POST); | |
$errores = array(); | |
$email_cliente = "[email protected]"; | |
//Validacion | |
if(strlen($nombre) < 2){ | |
array_push($errores, array("selector" => "error_nombre", "mensaje" => "Escribe tu nombre")); | |
} | |
if(strlen($telefono) < 2){ | |
array_push($errores, array("selector" => "error_telefono", "mensaje" => "Escribe tu teléfono")); | |
} | |
if(strlen($asunto) < 2){ | |
array_push($errores, array("selector" => "error_asunto", "mensaje" => "Escribe tu asunto")); | |
} | |
if(strlen($mensaje) < 2){ | |
array_push($errores, array("selector" => "error_mensaje", "mensaje" => "Escribe tu mensaje")); | |
} | |
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
array_push($errores, array("selector" => "error_email", "mensaje" => "Email invalido")); | |
} | |
if(count($errores) == 0){ | |
$f_mensaje = "<p>Nombre: ".$nombre."</p>"; | |
$f_mensaje .= "<p>Email: ".$email."</p>"; | |
$f_mensaje .= "<p>Teléfono: ".$telefono."</p>"; | |
$f_mensaje .= "<p>Asunto: ".$asunto."</p>"; | |
$f_mensaje .= "<p>Mensaje: ".$mensaje."</p>"; | |
sendEmail($email, $nombre, $email_cliente, "Subject", $f_mensaje); | |
echo json_encode(array("status" => "200")); | |
}else{ | |
echo json_encode(array("status" => 500, "errores" => $errores)); | |
} | |
function sendEmail($from_email, $from_name, $to_email, $to_name, $message){ | |
try { | |
$mandrillapi='-API-KEY-'; | |
$mandrill = new Mandrill($mandrillapi); | |
$template_content = array(); | |
$message = array( | |
'subject' => 'Contacto', | |
'from_email' => $from_email, | |
'from_name' => $from_name, | |
'to' => array( | |
array( | |
'email' => $to_email, | |
'name' => $to_name | |
) | |
), | |
'global_merge_vars' => array( | |
array( | |
'name' => 'CONTENIDO', | |
'content' => $message | |
) | |
), | |
'tags' => array('contacto'), | |
'subaccount' => 'general', | |
); | |
$async = false; | |
$result = $mandrill->messages->sendTemplate('basico-general', $template_content, $message, $async); | |
} catch(Mandrill_Error $e) { | |
echo "Error"; | |
} | |
} | |
?> |
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
$("#contact-form").submit(function(e){ | |
e.preventDefault(); | |
$(".error").hide(); | |
$this = $(this); | |
var data = $(this).serialize(), | |
url = $(this).attr("action"); | |
$.ajax(url,{ | |
data: data, | |
type: "POST", | |
dataType: "json" | |
}).done(function(data, status, objXHR){ | |
if(data.status == "200"){ | |
$this.slideUp('fast',function(){ | |
$this.html("<h2 class='success'>Mensaje enviado con exito</h2>").slideDown(); | |
}); | |
}else if(data.status == "500"){ | |
$.each(data.errores,function(i,e){ | |
$("."+e.selector).show(); | |
}); | |
} | |
}).fail(function(objXHR, status){ | |
console.log(objXHR); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment