Skip to content

Instantly share code, notes, and snippets.

@jorgechavz
Last active January 7, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgechavz/2a1e6c338b692f5d331d to your computer and use it in GitHub Desktop.
Save jorgechavz/2a1e6c338b692f5d331d to your computer and use it in GitHub Desktop.
Contact mail System
<?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 = "contacto@website.mx";
//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";
}
}
?>
$("#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