enviarCorreoContacto.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\SMTP;
  4. use PHPMailer\PHPMailer\Exception;
  5. require '../vendor/autoload.php';
  6. header("Content-type: application/json; charset=utf-8");
  7. $jsonSolicitud = json_decode(file_get_contents('php://input'), true);
  8. if (count($jsonSolicitud) > 0) {
  9. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  10. $idioma = $jsonSolicitud['idioma'];
  11. $necesito = $jsonSolicitud['necesito'];
  12. $nombre = $jsonSolicitud['nombre'];
  13. $apellidos = $jsonSolicitud['apellido'];
  14. $cargo = $jsonSolicitud['cargo'];
  15. $empresa = $jsonSolicitud['empresa'];
  16. $pais = $jsonSolicitud['pais'];
  17. $telefono = $jsonSolicitud['telefono'];
  18. if(empty($jsonSolicitud['estado'])){
  19. $estado = '';
  20. }else{
  21. $estado = $jsonSolicitud['estado'];
  22. }
  23. $email = $jsonSolicitud['email'];
  24. $comentarios = $jsonSolicitud['comentarios'];
  25. $datetime = strftime("%d/%m/%Y %H:%M");
  26. $recaptcha_response = $jsonSolicitud['captcha'];
  27. if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  28. $ip = $_SERVER['HTTP_CLIENT_IP'];
  29. }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  30. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  31. }else{
  32. $ip = $_SERVER['REMOTE_ADDR'];
  33. }
  34. // Realizamos la petición de control:
  35. $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
  36. $recaptcha_secret = '6LfGubMZAAAAALSNDYiOV44Me6zPJ-0e0gak1x-u';
  37. $recaptchaGet = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
  38. $recaptcha = json_decode($recaptchaGet,true);
  39. // Miramos si se considera humano o robot:
  40. // echo json_encode($recaptcha);
  41. if ($recaptcha['success'] == true) {
  42. if($recaptcha['score'] >= 0.1){
  43. // enviamos el correo
  44. enviarCorreo($necesito,$nombre,$apellidos,$cargo,$empresa,$pais,$telefono,$estado,$email,$comentarios,$ip,$datetime,$idioma);
  45. }else{
  46. if($idioma == 'es'){
  47. $msj = "No hemos podido descubrir que eres un humano, por favor recarga la página";
  48. }else if($idioma == 'en'){
  49. $msj = "We have not been able to discover that you are a human, please reload the page";
  50. }
  51. controlError(false, $msj);
  52. }
  53. }else{
  54. if($idioma == 'es'){
  55. $msj = "Captcha repetido o invalido, favor de recargar la Página.";
  56. }else if($idioma == 'en'){
  57. $msj = "Repeated or invalid Captcha, please reload the page.";
  58. }
  59. controlError(false,$msj);
  60. }
  61. }
  62. }
  63. function enviarCorreo($necesito,$nombre,$apellidos,$cargo,$empresa,$pais,$telefono,$estado,$email,$comentarios,$ip,$datetime,$idioma){
  64. $mail = new PHPMailer;
  65. //Enable SMTP debugging.
  66. $mail->SMTPDebug = 3;
  67. //Set PHPMailer to use SMTP.
  68. $mail->isSMTP();
  69. //Set SMTP host name
  70. $mail->Host = "smtp.com";
  71. //Set this to true if SMTP host requires authentication to send email
  72. $mail->SMTPAuth = true;
  73. //Provide username and password
  74. $mail->Username = "web.plataforma.educativa@ittec.mx";
  75. $mail->Password = "UJG4bH2Z3S^C";
  76. //If SMTP requires TLS encryption then set it
  77. $mail->SMTPSecure = "tls";
  78. //Set TCP port to connect to
  79. $mail->Port = 2525;
  80. $mail->From = "web.plataforma.educativa@ittec.mx";
  81. $mail->FromName = "Sitio Web ITTEC";
  82. $mail->addAddress("alejandro.l@ittec.mx","Centro de Soluciones");
  83. // $mail->addAddress("soluciones@ittec.mx","Centro de Soluciones");
  84. $mail->isHTML(true);
  85. $mail->Subject = "Contacto - ".$necesito;
  86. $mail->Body = "<strong>IP: </strong>".$ip."<br>".
  87. "<strong>Nombre: </strong>".$nombre." ".$apellidos."<br>".
  88. "<strong>Cargo: </strong>".$cargo."<br>".
  89. "<strong>Empresa: </strong>".$empresa."<br>".
  90. "<strong>País: </strong>".$pais."<br>".
  91. "<strong>Teléfono: </strong>".$telefono."<br>".
  92. "<strong>Estado: </strong>".$estado."<br>".
  93. "<strong>Correo electónico: </strong>".$email."<br>".
  94. "<strong>Fecha y Hora: </strong>".$datetime."<br>".
  95. "<strong>Comentarios: </strong>".$comentarios."<br>";
  96. $mail->CharSet = 'UTF-8';
  97. $mail->SMTPDebug = 0;
  98. // $mail->AltBody = "This is the plain text version of the email content";
  99. if(!$mail->send()){
  100. if($idioma == 'es'){
  101. $msj = "No se ha podido enviar el email, intenta más tarde.";
  102. }else if($idioma == 'en'){
  103. $msj = "The email could not be sent, please try again later.";
  104. }
  105. controlError(false, $msj);
  106. // echo "Mailer Error: " . $mail->ErrorInfo;
  107. }else{
  108. if($idioma == 'es'){
  109. $msj = "La demanda de contacto ha sido enviada exitosamente.";
  110. }else if($idioma == 'en'){
  111. $msj = "The contact request has been sent successfully.";
  112. }
  113. controlError(true, $msj);
  114. }
  115. }
  116. function controlError($succes,$mensaje) {
  117. $response = array('success'=>$succes,'mensaje'=>$mensaje);
  118. echo json_encode($response);
  119. }
  120. ?>