如何禁用PHPMailer重定向

时间:2014-11-23 13:41:42

标签: php forms email submit phpmailer

我有邮件表格:

  <div class="contact">
    <form id="mailer-form" action="./plugins/mailer/gmail.php" method="post" name="message">
      <input type="text" name="name" /> <span>Jméno <strong>*</strong></span>
      <div class="clear"></div>
      <input type="text" name="email" /> <span>Email <strong>*</strong></span>
      <div class="clear"></div>
      <input type="text" name="subject" /> <span>Předmět</span>
      <input type="hidden" name="frompage" value="yes" />
      <div class="clear"></div>
      <span>Zpráva <strong> * </strong></span>
      <div class="clear"></div>
      <textarea name="message" id="message"></textarea><br />
      <input type="submit" name="submit" value="Odeslat zprávu!" />
    </form>
  </div>

和PHPMailer脚本:

<?php 
if ($_POST['frompage'] == "yes")
{
    date_default_timezone_set('Etc/UTC');

    require './PHPMailerAutoload.php';

    //Create a new PHPMailer instance
    $mail = new PHPMailer;

    //Tell PHPMailer to use SMTP
    $mail->isSMTP();

    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;

    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 587;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';

    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;

    $mail->CharSet = 'UTF-8';

    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "xxx";

    //Password to use for SMTP authentication
    $mail->Password = "xxx";

    //Set who the message is to be sent from
    $mail->setFrom($_POST['email'], $_POST['name']);

    //Set an alternative reply-to address
    $mail->addReplyTo('xxx', 'xxx');

    //Set who the message is to be sent to
    $mail->addAddress('xxx', 'xxx');

    //Set the subject line
    $mail->Subject = $_POST['subject'];

    $mail->msgHTML($_POST['message']);

    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
?>

发送电子邮件工作正常,但在发送电子邮件后,页面会将我重定向到带有Message sent echo的gmail.php。

如何使用提交按钮的值响应而无法停留在index.php上?

感谢。

2 个答案:

答案 0 :(得分:1)

它没有重定向您,您将表单操作设置为&#34; ./ plugins / mailer / gmail.php&#34;,这是表单数据发送的位置。

答案 1 :(得分:0)

替换:

echo "Message sent!";

通过

header("Location:YourFormPage.php");

它会把你送回去。你可以做你想要的事情,你需要研究一下AJAX,所以使用这个就没有必要离开第一页并完成表格底层。