Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HTML
- <form role="form" method="post" id="contact-form" action="php/contact.php">
- <div class="col-md-7 lightGreyBg">
- <div class="messages" role="alert"></div>
- <div class="controls">
- <div class="col-md-5 form-group formBox whiteBg">
- <label for="firstname" class="formLabel"><i class="fa fa-user"></i></label>
- <input type="text" class="formInput" name="name" placeholder="Nombre y Apellido" tabindex="1" required>
- <div class="help-block with-errors"></div>
- </div>
- <div class="col-md-5 form-group formBox whiteBg">
- <label for="email" class="formLabel"><i class="fa fa-envelope-o"></i></label>
- <input type="email" class="formInput" name="mail" placeholder="Email" tabindex="2" required>
- <div class="help-block with-errors"></div>
- </div>
- <div class="col-md-5 form-group formBox whiteBg">
- <label for="phone" class="formLabel"><i class="fa fa-phone"></i></label>
- <input type="tel" class="formInput" name="phone" placeholder="Teléfono" tabindex="3" required>
- <div class="help-block with-errors"></div>
- </div>
- <div class="col-md-5 form-group formBox whiteBg">
- <label for="city" class="formLabel"><i class="fa fa-globe"></i></label>
- <input type="text" class="formInput" name="city" placeholder="Localidad" tabindex="4" required>
- <div class="help-block with-errors"></div>
- </div>
- <div class="col-md-5 form-group formBox whiteBg">
- <label class="formLabel" for="area"><i class="fa fa-caret-down"></i></label>
- <select class="formInput" name="area" tabindex="5" required>
- <option value="">Seleccione un destinatario</option>
- <option value="admin">Administración</option>
- <option value="venta">Ventas</option>
- <option value="posventa">Posventa</option>
- <option value="repuestos">Repuestos</option>
- </select>
- <div class="help-block with-errors"></div>
- </div>
- <div class="col-md-5 form-group formBox whiteBg">
- <textarea class="formInput"placeholder="Mensaje" tabindex="8" rows="5" name="comment"></textarea>
- </div>
- <div class="col-md-5 form-group paddingT25">
- <button type="submit" id="form-submit" class="btn btn-default" tabindex="10" style="float:right;"> Enviar</button>
- </div>
- </div>
- </div>
- </form>
- $(function () {
- // init the validator
- // validator files are included in the download package
- // otherwise download from http://1000hz.github.io/bootstrap-validator
- $('#contact-form').validator();
- // when the form is submitted
- $('#contact-form').on('submit', function (e) {
- // if the validator does not prevent form submit
- if (!e.isDefaultPrevented()) {
- var url = "php/contact.php";
- // POST values in the background the the script URL
- $.ajax({
- type: "POST",
- url: url,
- data: $(this).serialize(),
- success: function (data)
- {
- // data = JSON object that contact.php returns
- // we recieve the type of the message: success x danger and apply it to the
- var messageAlert = 'alert-' + data.type;
- var messageText = data.message;
- // let's compose Bootstrap alert box HTML
- var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
- // If we have messageAlert and messageText
- if (messageAlert && messageText) {
- // inject the alert to .messages div in our form
- $('#contact-form').find('.messages').html(alertBox);
- // empty the form
- $('#contact-form')[0].reset();
- }
- }
- });
- return false;
- }
- })
- });
- <?php
- /**
- * @version 1.0
- */
- require("class.phpmailer.php");
- require("class.smtp.php");
- // form field names and their translations.
- // array variable name => Text to appear in the email
- $fields = array('area' => 'Destinatario', 'name' => 'Nombre', 'phone' => 'Teléfono', 'mail' => 'Email', 'city' => 'Ciudad', 'comment' => 'Mensaje');
- // message that will be displayed when everything is OK :)
- $okMessage = 'La consulta se envió correctamente. Nos pondremos en contacto contigo a la brevedad, muchas gracias.';
- // If something goes wrong, we will display this message.
- $errorMessage = 'Se ha producido un error al enviar la consulta. Por favor intentalo de nuevo más tarde. Muchas gracias.';
- // Datos de la cuenta de correo utilizada para enviar vía SMTP
- $smtpHost = "account@host.com"; // Dominio alternativo brindado en el email de alta
- $smtpUsuario = "info@user.com"; // Mi cuenta de correo
- $smtpClave = "Passwor"; // Mi contraseña
- // Email donde se enviaran los datos cargados en el formulario de contacto
- $emailDestino = "example@gmail.com";
- $mail = new PHPMailer();
- $mail->IsSMTP();
- $mail->SMTPAuth = true;
- $mail->Port = 587;
- $mail->IsHTML(true);
- $mail->CharSet = "utf-8";
- $mail->Host = $smtpHost;
- $mail->Username = $smtpUsuario;
- $mail->Password = $smtpClave;
- try
- {
- if(count($_POST) == 0) throw new Exception('Form is empty');
- // Valores enviados desde el formulario
- $nombre = $_POST["name"];
- $email = $_POST["mail"];
- $mensaje = $_POST["comment"];
- $emailText = "Haz recibido una nueva consultan=============================n";
- foreach ($_POST as $key => $value) {
- // If the field exists in the $fields array, include it in the email
- if (isset($fields[$key])) {
- $emailText .= "$fields[$key]: $valuen";
- }
- }
- $mail->From = $smtpUsuario; // Email desde donde envío el correo.
- $mail->FromName = $nombre;
- $mail->AddAddress($emailDestino); // Esta es la dirección a donde enviamos los datos del formulario
- $mail->AddReplyTo($email); // Esto es para que al recibir el correo y poner Responder, lo haga a la cuenta del visitante.
- $mail->Subject = "Nueva consulta desde la Web"; // Este es el titulo del email.
- $mensajeHtml = nl2br($emailText);
- $mail->Body = "{$mensajeHtml} <br /><br />Formulario de contacto de Toyota Del Parque.<br />"; // Texto del email en formato HTML
- $mail->AltBody = "{$mensaje} nn Formulario de contacto de Toyota Del Parque."; // Texto sin formato HTML
- // FIN - VALORES A MODIFICAR //
- $mail->SMTPOptions = array(
- 'ssl' => array(
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- 'allow_self_signed' => true
- )
- );
- $estadoEnvio = $mail->Send();
- if($estadoEnvio){
- $responseArray = array('type' => 'success', 'message' => $okMessage);
- } else {
- $responseArray = array('type' => 'danger', 'message' => $errorMessage);
- }
- }
- catch (Exception $e)
- {
- $responseArray = array('type' => 'danger', 'message' => $errorMessage);
- }
- // if requested by AJAX request return JSON response
- if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
- $encoded = json_encode($responseArray);
- header('Content-Type: application/json');
- echo $encoded;
- }
- // else just display the message
- else {
- echo $responseArray['message'];
- }
- $estadoEnvio = $mail->Send();
- if($estadoEnvio){
- echo "El correo fue enviado correctamente.";
- } else {
- echo "Ocurrió un error inesperado.";
- }
Add Comment
Please, Sign In to add comment