【发布时间】:2022-06-16 23:15:20
【问题描述】:
我在处理电子邮件后无法让 php 重定向。我已经尝试了this page 中提到的所有解决方案,以及一些没有运气的涉及 javascript 的解决方案。以下是我的代码的相关部分:
function redirect($url) {
ob_start();
header('Location: '.$url);
ob_end_flush();
exit();
}
if ( isset($_REQUEST['sendemail']) ) {
header("Content-Type: text/plain");
header("X-Node: $hostname");
$from = $_REQUEST['from'];
$name = $_REQUEST['name'];
$toemail = "djsuson@gmail.com";
$subject = "Customer question";
$message = $_REQUEST['message'];
ob_start(); //start capturing output buffer because we want to change output to html
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->IsSMTP();
if ( strpos($hostname, 'cpnl') === FALSE ) //if not cPanel
$mail->Host = 'relay-hosting.secureserver.net';
else
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->From = $from;
$mail->FromName = $name;
$mail->AddAddress($toemail);
$mail->Subject = $subject;
$mail->Body = $message;
$mailresult = $mail->Send();
$mailconversation = nl2br(htmlspecialchars(ob_get_clean()));
if ( !$mailresult ) {
echo 'FAIL: ' . $mail->ErrorInfo . '<br />' . $mailconversation;
redirect('http://otterlywoods.com/failure.html');
} else {
echo $mailconversation;
redirect('http://otterlywoods.com/success.html');
}
对此的任何帮助将不胜感激。
【问题讨论】:
-
如果您在编码时显示 PHP 错误或查看生产站点上的错误日志,您应该会看到 headers already sent error message。必须在回显任何正文消息之前发送标头。您应该重新设计您的流程(阅读链接)。