Search code examples
phpphpmailer

Disable PHPMailer Redirection


I have a page containing a mail form.

 <section class="contato" id="inicio-form">
        <div class="container">
            <h2 class="heading-form" tabindex="-1">Descubra o que a Softcom pode fazer pelo seu negócio</h2>
            <form action="app/mail.php" method="POST" class="js-form" _lpchecked="1">
            <div class="form-group row">
            <div class="col-sm-12 text-center">
                <input type="text" name="nome" class="form-control form-control-sm required" id="colFormLabelSm" placeholder="Nome" style="background-image: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABHklEQVQ4EaVTO26DQBD1ohQWaS2lg9JybZ+AK7hNwx2oIoVf4UPQ0Lj1FdKktevIpel8AKNUkDcWMxpgSaIEaTVv3sx7uztiTdu2s/98DywOw3Dued4Who/M2aIx5lZV1aEsy0+qiwHELyi+Ytl0PQ69SxAxkWIA4RMRTdNsKE59juMcuZd6xIAFeZ6fGCdJ8kY4y7KAuTRNGd7jyEBXsdOPE3a0QGPsniOnnYMO67LgSQN9T41F2QGrQRRFCwyzoIF2qyBuKKbcOgPXdVeY9rMWgNsjf9ccYesJhk3f5dYT1HX9gR0LLQR30TnjkUEcx2uIuS4RnI+aj6sJR0AM8AaumPaM/rRehyWhXqbFAA9kh3/8/NvHxAYGAsZ/il8IalkCLBfNVAAAAABJRU5ErkJggg==&quot;); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;">
            </div>
            </div>
            <div class="form-group row">
            <div class="col-sm-12 text-center">
                <input type="email" name="email" class="form-control form-control-sm required" id="colFormLabel" placeholder="E-mail">
            </div>
            </div>
            <div class="form-group row">
            <div class="col-sm-12 text-center">
                <input type="text" name="telefone" class="form-control form-control-sm mask-phone required" id="colFormLabelLg" placeholder="Telefone">
            </div>
            </div>
            <div class="form-group row">
            <div class="col-sm-12 text-center">
                <input type="text" name="cidade" class="form-control form-control-sm required" id="colFormLabelLg" placeholder="Cidade">
            </div>
            </div>

            <button type="submit" class="btn btn-dark js-submit" data-text="Solicite uma ativação">Solicite uma ativação</button>

            <div class="msg">
            <p class="msg-error" style="display: none;">Preencha todos os campos corretamente.</p>
            <p class="msg-success" style="display: none;">Mensagem enviada com sucesso!</p>
            </div>

        </form>
        </div>
    </section>

And this PHPMailer script:

    <?php
require 'PHPMailer/PHPMailerAutoload.php';

$nome = utf8_decode($_POST['nome']);
$email = $_POST['email'];
$celular = trim($_POST['telefone'], '_');
$cidade = utf8_decode($_POST['cidade']);

  $content = '
    <h1>Contato Landing Page - Meu Carrinho</h1>
    <table>
      <tr>
        <td><b>Nome</b></td>
        <td>' . $nome . '</td>
      </tr>
      <tr>
        <td><b>E-mail</b></td>
        <td>' . $email . '</td>
      </tr>
      <tr>
        <td><b>Telefone</b></td>
        <td>' . $celular . '</td>
      </tr>
      <tr>
      <tr>
        <td><b>Cidade</b></td>
        <td>' . $cidade . '</td>
      </tr>
    </table>
  ';

 $mail = new PHPMailer;

  $mail->IsSMTP();
  $mail->Host = 'xxx';
  $mail->Username = 'xxx';
  $mail->Password = 'xxx';
  $mail->Port = xxx;
  $mail->SMTPSecure = 'xxx';

  $mail->SMTPDebug = 0;
  $mail->SMTPAuth = true;

  $mail->isHTML(true);
  $mail->CharSet = 'UTF-8';
  $mail->Subject =  "xxx";
  $mail->From = 'xxx';
  $mail->FromName = 'xxx';
  $mail->Body = $content;

  $mail->addAddress('xxx@xxx.xx');
  $mail->addAddress('xxx@xxx.xx');

  if(!$mail->send()) {

    $retorno = array(
      'success' => false,
      'message' => 'Não foi possível enviar email.',
    );
  } else {

    if($mail->send()){
      $retorno = array(
        'success' => true,
        'message' => 'Email enviado com sucesso.',
      );
    }

  }

die(json_encode($retorno));

Sending emails is working normally, but the script is redirecting me to a mail.php link with an echo message ({"success":true,"message":"Email enviado com sucesso."}).

How can I disable it and stay at the same page or how can I display this message on the same page?

Thanks!


Solution

  • This is because you are passing a json objct as a parameter in die function. This parameter represents the message to printed while exiting from script. Just remove the parameter from die function.