enviarCorreoContacto.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\SMTP;
  4. use PHPMailer\PHPMailer\Exception;
  5. require '../vendor/autoload.php';
  6. if (isset($_POST['necesito'])) {
  7. $necesito = $_POST['necesito'];
  8. $nombre = $_POST['nombre'];
  9. $apellidos = $_POST['apellido'];
  10. $cargo = $_POST['cargo'];
  11. $empresa = $_POST['empresa'];
  12. $direccion = $_POST['direccion'];
  13. $cp = $_POST['cp'];
  14. $ciudad = $_POST['ciudad'];
  15. $pais = $_POST['pais'];
  16. $telefono = $_POST['telefono'];
  17. if(empty($_POST['estado'])){
  18. $estado = '';
  19. }else{
  20. $estado = $_POST['estado'];
  21. }
  22. $email = $_POST['email'];
  23. $comentarios = $_POST['comentarios'];
  24. $datetime = strftime("%d/%m/%Y %H:%M");
  25. $recaptcha_response = $_POST['recaptcha_response'];
  26. if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  27. $ip = $_SERVER['HTTP_CLIENT_IP'];
  28. }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  29. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  30. }else{
  31. $ip = $_SERVER['REMOTE_ADDR'];
  32. }
  33. // Realizamos la petición de control:
  34. $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
  35. $recaptcha_secret = '6LelvrMZAAAAAP1h1tJjDhephX1Vy8980qKT4ONn';
  36. $recaptchaGet = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
  37. $recaptcha = json_decode($recaptchaGet,true);
  38. // Miramos si se considera humano o robot:
  39. // echo json_encode($recaptcha);
  40. if ($recaptcha['success'] == true) {
  41. if($recaptcha['score'] >= 0.1){
  42. // enviamos el correo
  43. enviarCorreo($necesito,$nombre,$apellidos,$cargo,$empresa,$direccion,$cp,$ciudad,$pais,$telefono,$estado,$email,$comentarios,$ip,$datetime);
  44. }else{
  45. controlError(false,"No hemos podido descubrir que eres un humano, por favor recarga la Página");
  46. }
  47. }else{
  48. controlError(false,"Captcha repetido o invalido, favor de recargar la Página.");
  49. }
  50. }
  51. function enviarCorreo($necesito,$nombre,$apellidos,$cargo,$empresa,$direccion,$cp,$ciudad,$pais,$telefono,$estado,$email,$comentarios,$ip,$datetime){
  52. $mail = new PHPMailer;
  53. //Enable SMTP debugging.
  54. $mail->SMTPDebug = 3;
  55. //Set PHPMailer to use SMTP.
  56. $mail->isSMTP();
  57. //Set SMTP host name
  58. $mail->Host = "smtp.com";
  59. //Set this to true if SMTP host requires authentication to send email
  60. $mail->SMTPAuth = true;
  61. //Provide username and password
  62. $mail->Username = "web.plataforma.educativa@ittec.mx";
  63. $mail->Password = "UJG4bH2Z3S^C";
  64. //If SMTP requires TLS encryption then set it
  65. $mail->SMTPSecure = "tls";
  66. //Set TCP port to connect to
  67. $mail->Port = 2525;
  68. $mail->From = "web.plataforma.educativa@ittec.mx";
  69. $mail->FromName = "Sitio Web Plataforma Educativa";
  70. // $mail->addAddress("alejandro.l@ittec.mx","Centro de Soluciones");
  71. $mail->addAddress("soluciones@ittec.mx","Centro de Soluciones");
  72. $mail->isHTML(true);
  73. $mail->Subject = "Contacto - ".$necesito;
  74. $mail->Body = "<strong>IP: </strong>".$ip."<br>".
  75. "<strong>Nombre: </strong>".$nombre." ".$apellidos."<br>".
  76. "<strong>Cargo: </strong>".$cargo."<br>".
  77. "<strong>Institución Educativa: </strong>".$empresa."<br>".
  78. "<strong>Dirección: </strong>".$direccion.", ".$cp."<br>".
  79. "<strong>Ciudad: </strong>".$ciudad."<br>".
  80. "<strong>País: </strong>".$pais."<br>".
  81. "<strong>Teléfono: </strong>".$telefono."<br>".
  82. "<strong>Estado: </strong>".$estado."<br>".
  83. "<strong>Correo electónico: </strong>".$email."<br>".
  84. "<strong>Fecha y Hora: </strong>".$datetime."<br>".
  85. "<strong>Comentarios: </strong>".$comentarios."<br>";
  86. $mail->CharSet = 'UTF-8';
  87. $mail->SMTPDebug = 0;
  88. // $mail->AltBody = "This is the plain text version of the email content";
  89. if(!$mail->send()){
  90. controlError(false, "No se ha podido enviar el email, intenta más tarde.");
  91. // echo "Mailer Error: " . $mail->ErrorInfo;
  92. }
  93. else{
  94. controlError(true, "La demanda de contacto ha sido enviada exitosamente.");
  95. }
  96. }
  97. function controlError($succes,$mensaje) {
  98. $response = array('success'=>$succes,'mensaje'=>$mensaje);
  99. echo json_encode($response);
  100. }
  101. ?>