【问题标题】:Redirecting after sending Email with PHPMailer使用 PHPMailer 发送电子邮件后重定向
【发布时间】:2018-01-17 12:34:06
【问题描述】:

在发送带有PHPMailer 的确认电子邮件后,我遇到了重定向问题。用户不会被重定向到memberIndex.php

我试过了:

`$mail->Send();
header("Location:../memberIndex.php");
exit();`

同时关闭调试:

`$mail->SMTPDebug = 0;` 

以及与ob_start/flush/clean 等的所有组合

注意:当我将邮件 ($mail->Body) 的正文留空时,它会发送一封电子邮件并按应有的方式重定向。否则,它会停留在注册页面上。 在正文中,我有一些 HTML 标记和文本。莫非是body挡住了重定向?

这是我构建电子邮件的代码:

require '../../PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'host';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'username';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->setFrom('info@test.com', 'test');
$mail->addAddress('test@test.com');     // Add a recipient


$mail->isHTML(true);                                  // Set email format to HTML
$mail->AddEmbeddedImage('../img/text_dark.png', 'cs');
$mail->Subject = 'Hi Customer!';

$body = 'test';

$mail->Body = $body;

$mail->Send();
header("Location:../memberIndex.php");
exit();
?>

【问题讨论】:

  • 你能把整个代码贴在你正在构建你的电子邮件的地方吗?
  • “有没有可能,body 会阻止重定向?” - 仅当您实际输出它时,i 才会成为响应的一部分 ... // 请去阅读How to Ask,然后编辑你的问题,让它包含一些真正有用的细节。
  • @Kyborek 即使我将“测试”放入正文中,它也不会重定向..
  • @rey 您能否验证(使用开发人员工具)服务器实际上向您发送了 Location 标头?你能确认没有其他输出吗?您可以尝试使用这样的绝对网址吗? header("Location: https://stackoverflow.com/");
  • @LouysPatriceBessette 谢谢!在 PHPMailer 中一切正常。我想我现在必须检查我的代码是否有空格。

标签: javascript php jquery email phpmailer


【解决方案1】:

至少将$mail->send() 包装在一个 if 块中(try...catch 会更好),例如:

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    header("Location:../memberIndex.php");
}

了解可能出了什么问题。 还要查看PHP header manual. 最重要的是在发送标头之前不要有 any 输出。没有空白,没有错误,什么都没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多