Guest User

Untitled

a guest
Feb 3rd, 2018
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. HTML
  2. <form role="form" method="post" id="contact-form" action="php/contact.php">
  3. <div class="col-md-7 lightGreyBg">
  4. <div class="messages" role="alert"></div>
  5.  
  6. <div class="controls">
  7.  
  8. <div class="col-md-5 form-group formBox whiteBg">
  9. <label for="firstname" class="formLabel"><i class="fa fa-user"></i></label>
  10. <input type="text" class="formInput" name="name" placeholder="Nombre y Apellido" tabindex="1" required>
  11. <div class="help-block with-errors"></div>
  12. </div>
  13. <div class="col-md-5 form-group formBox whiteBg">
  14. <label for="email" class="formLabel"><i class="fa fa-envelope-o"></i></label>
  15. <input type="email" class="formInput" name="mail" placeholder="Email" tabindex="2" required>
  16. <div class="help-block with-errors"></div>
  17. </div>
  18. <div class="col-md-5 form-group formBox whiteBg">
  19. <label for="phone" class="formLabel"><i class="fa fa-phone"></i></label>
  20. <input type="tel" class="formInput" name="phone" placeholder="Teléfono" tabindex="3" required>
  21. <div class="help-block with-errors"></div>
  22. </div>
  23. <div class="col-md-5 form-group formBox whiteBg">
  24. <label for="city" class="formLabel"><i class="fa fa-globe"></i></label>
  25. <input type="text" class="formInput" name="city" placeholder="Localidad" tabindex="4" required>
  26. <div class="help-block with-errors"></div>
  27. </div>
  28. <div class="col-md-5 form-group formBox whiteBg">
  29. <label class="formLabel" for="area"><i class="fa fa-caret-down"></i></label>
  30. <select class="formInput" name="area" tabindex="5" required>
  31. <option value="">Seleccione un destinatario</option>
  32. <option value="admin">Administración</option>
  33. <option value="venta">Ventas</option>
  34. <option value="posventa">Posventa</option>
  35. <option value="repuestos">Repuestos</option>
  36. </select>
  37. <div class="help-block with-errors"></div>
  38. </div>
  39. <div class="col-md-5 form-group formBox whiteBg">
  40. <textarea class="formInput"placeholder="Mensaje" tabindex="8" rows="5" name="comment"></textarea>
  41. </div>
  42. <div class="col-md-5 form-group paddingT25">
  43. <button type="submit" id="form-submit" class="btn btn-default" tabindex="10" style="float:right;"> Enviar</button>
  44. </div>
  45. </div>
  46. </div>
  47. </form>
  48.  
  49. $(function () {
  50.  
  51. // init the validator
  52. // validator files are included in the download package
  53. // otherwise download from http://1000hz.github.io/bootstrap-validator
  54.  
  55. $('#contact-form').validator();
  56.  
  57.  
  58. // when the form is submitted
  59. $('#contact-form').on('submit', function (e) {
  60.  
  61. // if the validator does not prevent form submit
  62. if (!e.isDefaultPrevented()) {
  63. var url = "php/contact.php";
  64.  
  65. // POST values in the background the the script URL
  66. $.ajax({
  67. type: "POST",
  68. url: url,
  69. data: $(this).serialize(),
  70. success: function (data)
  71. {
  72. // data = JSON object that contact.php returns
  73.  
  74. // we recieve the type of the message: success x danger and apply it to the
  75. var messageAlert = 'alert-' + data.type;
  76. var messageText = data.message;
  77.  
  78. // let's compose Bootstrap alert box HTML
  79. var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
  80.  
  81. // If we have messageAlert and messageText
  82. if (messageAlert && messageText) {
  83. // inject the alert to .messages div in our form
  84. $('#contact-form').find('.messages').html(alertBox);
  85. // empty the form
  86. $('#contact-form')[0].reset();
  87. }
  88. }
  89. });
  90. return false;
  91. }
  92. })
  93. });
  94.  
  95. <?php
  96. /**
  97. * @version 1.0
  98. */
  99.  
  100. require("class.phpmailer.php");
  101. require("class.smtp.php");
  102.  
  103. // form field names and their translations.
  104. // array variable name => Text to appear in the email
  105. $fields = array('area' => 'Destinatario', 'name' => 'Nombre', 'phone' => 'Teléfono', 'mail' => 'Email', 'city' => 'Ciudad', 'comment' => 'Mensaje');
  106.  
  107. // message that will be displayed when everything is OK :)
  108. $okMessage = 'La consulta se envió correctamente. Nos pondremos en contacto contigo a la brevedad, muchas gracias.';
  109.  
  110. // If something goes wrong, we will display this message.
  111. $errorMessage = 'Se ha producido un error al enviar la consulta. Por favor intentalo de nuevo más tarde. Muchas gracias.';
  112.  
  113.  
  114. // Datos de la cuenta de correo utilizada para enviar vía SMTP
  115. $smtpHost = "account@host.com"; // Dominio alternativo brindado en el email de alta
  116. $smtpUsuario = "info@user.com"; // Mi cuenta de correo
  117. $smtpClave = "Passwor"; // Mi contraseña
  118.  
  119. // Email donde se enviaran los datos cargados en el formulario de contacto
  120. $emailDestino = "example@gmail.com";
  121.  
  122. $mail = new PHPMailer();
  123. $mail->IsSMTP();
  124. $mail->SMTPAuth = true;
  125. $mail->Port = 587;
  126. $mail->IsHTML(true);
  127. $mail->CharSet = "utf-8";
  128.  
  129. $mail->Host = $smtpHost;
  130. $mail->Username = $smtpUsuario;
  131. $mail->Password = $smtpClave;
  132. try
  133. {
  134.  
  135. if(count($_POST) == 0) throw new Exception('Form is empty');
  136. // Valores enviados desde el formulario
  137.  
  138. $nombre = $_POST["name"];
  139. $email = $_POST["mail"];
  140. $mensaje = $_POST["comment"];
  141. $emailText = "Haz recibido una nueva consultan=============================n";
  142.  
  143. foreach ($_POST as $key => $value) {
  144. // If the field exists in the $fields array, include it in the email
  145. if (isset($fields[$key])) {
  146. $emailText .= "$fields[$key]: $valuen";
  147. }
  148. }
  149.  
  150. $mail->From = $smtpUsuario; // Email desde donde envío el correo.
  151. $mail->FromName = $nombre;
  152. $mail->AddAddress($emailDestino); // Esta es la dirección a donde enviamos los datos del formulario
  153. $mail->AddReplyTo($email); // Esto es para que al recibir el correo y poner Responder, lo haga a la cuenta del visitante.
  154. $mail->Subject = "Nueva consulta desde la Web"; // Este es el titulo del email.
  155. $mensajeHtml = nl2br($emailText);
  156. $mail->Body = "{$mensajeHtml} <br /><br />Formulario de contacto de Toyota Del Parque.<br />"; // Texto del email en formato HTML
  157. $mail->AltBody = "{$mensaje} nn Formulario de contacto de Toyota Del Parque."; // Texto sin formato HTML
  158. // FIN - VALORES A MODIFICAR //
  159.  
  160. $mail->SMTPOptions = array(
  161. 'ssl' => array(
  162. 'verify_peer' => false,
  163. 'verify_peer_name' => false,
  164. 'allow_self_signed' => true
  165. )
  166. );
  167. $estadoEnvio = $mail->Send();
  168. if($estadoEnvio){
  169. $responseArray = array('type' => 'success', 'message' => $okMessage);
  170. } else {
  171. $responseArray = array('type' => 'danger', 'message' => $errorMessage);
  172. }
  173. }
  174. catch (Exception $e)
  175. {
  176. $responseArray = array('type' => 'danger', 'message' => $errorMessage);
  177. }
  178.  
  179.  
  180. // if requested by AJAX request return JSON response
  181. if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  182. $encoded = json_encode($responseArray);
  183.  
  184. header('Content-Type: application/json');
  185.  
  186. echo $encoded;
  187. }
  188. // else just display the message
  189. else {
  190. echo $responseArray['message'];
  191. }
  192.  
  193.  
  194. $estadoEnvio = $mail->Send();
  195. if($estadoEnvio){
  196. echo "El correo fue enviado correctamente.";
  197. } else {
  198. echo "Ocurrió un error inesperado.";
  199. }
Add Comment
Please, Sign In to add comment