You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP邮件发送成功后无法跳转至指定页面技术求助

邮件发送成功后无法跳转指定页面的解决方案

这是个很常见的PHP跳转问题——你在发送HTTP跳转指令(header())之前已经向浏览器输出了内容,导致跳转直接失效了。

问题根源

PHP的header()函数有个严格要求:必须在任何实际输出(包括echo输出的文本、HTML标签,甚至文件开头的空格/UTF-8隐形BOM)之前调用。你的代码里先执行了echo "message sent";,这会让浏览器先收到响应内容,HTTP响应头已经被发送出去,后续的跳转指令自然就被浏览器忽略了。

解决方案

这里有两种实用的处理方式,你可以根据需求选择:

方案1:直接跳转(不显示提示文本)

移除echo语句,确保跳转前没有任何输出,并且在header后终止脚本执行,避免后续代码干扰:

if(!$mail->Send()) {
    echo "Error while sending email";
} else {
    header("Location: contact.html");
    exit; // 必须添加,强制终止脚本,防止后续代码执行
}

方案2:显示提示后延迟跳转

如果需要让用户看到"message sent"的提示再跳转,可以用JavaScript实现延迟跳转,这种方式不受HTTP头的限制:

if(!$mail->Send()) {
    echo "Error while sending email";
} else {
    echo "message sent";
    // 2秒后自动跳转到contact.html,可根据需求调整时间
    echo '<script type="text/javascript">
            setTimeout(function() {
                window.location.href = "contact.html";
            }, 2000);
          </script>';
}

额外注意事项

  • 检查你的PHP文件开头是否有多余的空格、换行,或者是否使用了带UTF-8 BOM的编码格式,这些隐形输出也会导致header()失效。
  • 生产环境记得把SMTPDebug设置为0,避免调试信息输出影响跳转或页面展示。

附你提供的代码与日志

原代码

<?php
require_once('class.phpmailer.php');
// include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$name = $_POST['form_name'];
$email = $_POST['form_email'];
$phone = $_POST['form_phone'];
$subject = $_POST['form_subject'];
$messages = $_POST['form_message'];

$mail = new PHPMailer();
$smtp_host = "HOST";
$smtp_user = "USER";
$smtp_password = "SECRET";
$smtp_port = "25";
$mail_from = $email;
$mail_to = "MAILTO";
$mail_to_name = "MAILNAME";

$message = array();
$message[]= 'Name : '.trim($name).' ';
$message[]='Phone : '.trim($phone).' ';
$message[]='Email : '.trim($email).' ';
$message[]='Message : '.trim($messages).' ';
$message = implode('<br/>', $message);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "{$smtp_host}"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "{$smtp_host}"; // sets the SMTP server
$mail->Port = $smtp_port; // set the SMTP port for the GMAIL server
$mail->Username = "{$smtp_user}"; // SMTP account username
$mail->Password = "{$smtp_password}"; // SMTP account password
$mail->SetFrom("{$mail_from}", "{$mail_from_name}");
$mail->Subject = "$subject";
$mail->MsgHTML($message);

$address = "{$mail_to}";
$mail->AddAddress($address, "{$mail_to_name}" );

if(!$mail->Send()) {
    echo "Error while sending email";
} else {
    echo "message sent";
    header("Location:contact.html");
}
?>

SMTP调试日志

SMTP -> FROM SERVER:220-cp.rdsindia.in ESMTP Exim 4.91 #1 Tue, 19 Mar 2019 18:45:10 +0530
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.
SMTP -> FROM SERVER: 250-cp.rdsindia.in Hello rdaconsulting.in [64.62.254.150]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
SMTP -> FROM SERVER:250 OK
SMTP -> FROM SERVER:250 Accepted
SMTP -> FROM SERVER:354 Enter message, ending with "." on a line by itself
SMTP -> FROM SERVER:250 OK id=1h6EaE-0005nk-5J
SMTP -> FROM SERVER:221 cp.rdsindia.in closing connection

内容的提问来源于stack exchange,提问作者tester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:46:01