发送电子邮件后的标题位置


Header location after sending email

我试图在提交表单后将用户重定向到感谢页面,但我的标题位置似乎不起作用(本地或在线)。当点击提交按钮时,什么也没有发生(尽管电子邮件被发送)。

我的php是这样的:

<?php
$val= $_POST['val'];
$toemail='mail@mail.com';
$name = $val['name'];
$societe = $val['societe'];
$email = $val['email'];
$phone = $val['phone'];
$msg = $val['msg'];
$subject = 'Contact';
$headers = "From: $email 'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=utf-8'r'n";
$message = "<b>Nom : </b>".$name."<br>";
$message .='<b>Societe : </b>'.$societe."<br>";
$message .='<b>Email : </b>'.$email."<br>";
$message .='<b>Telephone : </b>'.$phone."<br>";
$message .='<b>Message : </b>'.$msg;
mail($toemail, $subject, $message, $headers);
header("Location: http://example.com/thankyou.html");
?>

我做错了什么?提前感谢您的帮助。

编辑:谢谢你的帮助。如果我打开错误报告,我得到:警告:无法修改标头信息-标头已经由(output started at/path/email.php:12)
发送

正如其他人提到的,脚本在尝试发送报头之前发送输出。修复它的最简单方法可能是缓冲email()的输出,然后发送报头,如下所示:

ob_start();
mail($toemail, $subject, $message, $headers);
ob_end_clean();
header("Location: http://example.com/thankyou.html");

问题是,您在重定向之前发送输出。

要么使用输出缓冲,要么在新的页面上执行。

http://ee1.php.net/manual/en/ref.outcontrol.php

非常简单的使用以下方法ob_start(); exit(header("Location: http://example.com/thankyou.html")); 发送/修改HTTP标头的函数必须在输出之前调用。

使用header(),您只能重定向到同一站点内的页面。

你可以尝试使用

header("Location: /thankyou.html");

希望能有所帮助。