Doubt with phpmailer

0

I am sending an email in PHP using the php mailer library but the problem is that I want the button to say loading while the email was sent and after it says it has successfully sent its mail, check the Inbox

PHP Code:

//send mails

$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Helo = "smtp.gmail.com"; // nombre del dominio de primer nivel
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; //servidor donde se enviara el email
$mail->Port = "465";
$mail->Username = "[email protected]"; //cuenta que enviara los correos
$mail->Password = "123456"; //contraseña de la cuenta
$mail->From = "[email protected]"; // correo de quien lo envia
$mail->FromName = 'titulo'; //nombre o titulo referente al destinatario
$mail->Subject = 'sub'; //titulo que aparece despues de quien lo envia
$mail->IsHTML(true);
$mail->AddAddress([email protected]); //para quien va
$mail->Body = "ok"; //cuerpo del mensaje
$mail->CharSet = 'UTF-8';

if(!$mail->Send()){
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
   /* aqui el mensaje que se a enviado exitosamente */
}

Javascript code:

var url = "a.php";
$.ajax({
  type: "POST",
  url: url,
  data: $("#3as3dsad22s1").serialize(),
  success: function(data) {
    $("#asd2a2sd23333asd").html(data);
  }
});
return false;

I appreciate the help

    
asked by Johendry Parra 12.06.2018 в 21:50
source

2 answers

0

You could only use the function beforeSend() of ajax to add the text of loading to the button and then, once it finishes executing, show the message already successful or not.

HTML

<button id="send-mail">Enviar mail</button>
<div id="status-mail"></div>

Javascript

$('#send-mail').on('click', function() {
  var url = "a.php";
  var self = $(this);
  var textButton = self.text();

  $.ajax({
    type: "POST",
    url: url,
    data: $("#miform").serialize(),
    beforeSend: function() {
      // cambio  el texto del botón antes de enviarlo por ajax
      self.text('loading...');
    },
    success: function(data) {
      // vuelvo a poner el botón el mismo texto que tenía
      self.text(textButton);

      // agrego el texto que devuelve el PHP
      $('#status-mail').text(data.msg);
    }
  });
});

PHP

//send mails

$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Helo = "smtp.gmail.com"; // nombre del dominio de primer nivel
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com"; //servidor donde se enviara el email
$mail->Port = "465";
$mail->Username = "[email protected]"; //cuenta que enviara los correos
$mail->Password = "123456"; //contraseña de la cuenta
$mail->From = "[email protected]"; // correo de quien lo envia
$mail->FromName = 'titulo'; //nombre o titulo referente al destinatario
$mail->Subject = 'sub'; //titulo que aparece despues de quien lo envia
$mail->IsHTML(true);
$mail->AddAddress([email protected]); //para quien va
$mail->Body = "ok"; //cuerpo del mensaje
$mail->CharSet = 'UTF-8';

if(!$mail->Send()){
    echo json_encode(array('msg' => 'Mailer Error: ' . $mail->ErrorInfo));
} else {
    /* aqui el mensaje que se a enviado exitosamente */
    echo json_encode(array('msg' => 'El mail se envió exitosamente.'));
}
    
answered by 12.06.2018 / 22:31
source
0

I hope you help. I think it would be something like this:

Suppose your button is:

<button id="btnEnviarMail">Enviar Mail</button>

Then in your method:

$('#btnEnviarMail').click( function() { 
$('#btnEnviarMail').text('Enviando correo...')
var url = "a.php";
$.ajax({
  type: "POST",
  url: url,
  data: $("#3as3dsad22s1").serialize(),
  success: function(data) {
    $('#btnEnviarMail').text('Correo enviado!')
  },
 error: function(data) {
 $('#btnEnviarMail').text('Error al enviar')
}
});
return false;
})    
    
answered by 12.06.2018 в 22:31