禁用PHPMailer重定向

问题描述 投票:0回答:2

我有一个页面包含一个邮件表格。

 <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>

还有这个PHPMailer脚本。

    <?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('[email protected]');
  $mail->addAddress('[email protected]');

  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));

发送邮件工作正常,但是这个脚本将我重定向到一个带有回音信息({"success":true, "message": "Email enviado com sucesso."})的mail.php链接。

我怎样才能禁用它并保持在同一页面,或者我怎样才能在同一页面显示这条消息?

谢谢!我有一个包含邮件表格的页面,它的内容是 "电子邮件":"Email enviado com sucesso"})。

php phpmailer
2个回答
0
投票

这是因为你在die函数中传递了一个json objct作为参数。这个参数代表的是退出脚本时要打印的信息。只要从die函数中删除这个参数就可以了。


0
投票

你问错了问题。PHPMailer绝对与重定向无关,这完全取决于你调用PHPMailer的脚本,这就是 写。

你的 PHP 脚本被 JS 调用,JS 期待一个有效的 JSON 响应,但你没有给它一个响应。这样做吧。

header('Content-type: application/json');
echo json_encode($retorno);

我还建议你返回一个合适的HTTP错误代码 - 不要返回一个... ... 200 OK 当出现错误时,返回一个 400 Bad request 或类似的,而不是依靠 success 属性。如果你不这样做,你可能会发现你的浏览器缓存了不该缓存的东西。


0
投票

只要删除代码。

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

取而代之的是你想重定向的链接。 要重定向,你可以使用... header() 功能。

例如:。

if($mail->send()){
  header("Location: Your_Preferable_PAGE.php");
}

最后,也去掉 die() 职能。

die(json_encode($retorno));
© www.soinside.com 2019 - 2024. All rights reserved.