我在使用 PHPMailer
发送确认电子邮件后遇到重定向问题。用户不会被重定向到 memberIndex.php
。
我尝试过:
`$mail->Send();
header("Location:../memberIndex.php");
exit();`
同时关闭调试:
`$mail->SMTPDebug = 0;`
以及与ob_start/flush/clean
等的所有组合
注意:当我将邮件正文 ($mail->Body
) 留空时,它会发送一封电子邮件并按照应有的方式进行重定向。否则,它会保留在注册页面上。
在正文中,我有一些 HTML 标签和文本。难道是主体阻止了重定向?
这是我构建电子邮件的代码:
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();
?>
最佳答案
至少将 $mail->send()
包裹在 if block 中(尝试...catch 会更好),如下所示:
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
header("Location:../memberIndex.php");
}
了解可能出了什么问题。 另请查看 PHP header manual.最重要的是在发送 header 之前不要有任何输出。没有空格,没有错误,什么都没有。
关于javascript - 使用 PHPMailer 发送电子邮件后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45593733/