Cannot modify header information with mail() and h

2019-09-19 19:18发布

This question already has an answer here:

I am sure you all know this error:

Warning: Cannot modify header information - headers already sent by (output started at /homepages/4/d374499247/htdocs/wp-content/themes/tyler/mail.php:1) in /homepages/4/d374499247/htdocs/wp-content/themes/tyler/mail.php on line 34

And this is the code:

if (strpos($email,'@') !== false)  {
    ob_start();
    mail($to,$subject,$message,$headers);
    header("Location: thankyou.html");
    ob_end_flush(); }

Could anyone offer any help here? I just want it to mail() the information and then go to the Location page

EDIT: Here is the entire code..

<?php
$to = 'myemail@gmail.com';
$subject = 'בקשת הצטרפות לרשימת תפוצה';
$message =
'בקשת הצטרפות לרשימת תפוצה' . "\n\n" .
'שם: ' . $_REQUEST['name'] . "\n\n" .
'דואר אלקטרוני: ' . $_REQUEST['email'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
            'Reply-To: ' . $email . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
// server-validation
if (strpos($email,'@') !== false){
    mail($to,$subject,$message,$headers);
    header("Location: thankyou.html");
} else {
    echo "<p class='error'>כתובת הדוא'ל שגויה.</p>";
}
?>

3条回答
孤傲高冷的网名
2楼-- · 2019-09-19 19:23

It's most likely there's a whitespace of some sort before your PHP tags (as the output starats at the very first line).

There can't be ANY output before the header(), not even whitespaces or line breaks.

查看更多
一夜七次
3楼-- · 2019-09-19 19:25

This error is being caused by the fact that you have already sent data to the client at the moment you are trying to send the headers (which should be sent first).

Most likely, you have some whitespace in the beginning of your file. Based on your current code example, it is hard to say for sure what exactly is causing the problem, but based on the fact that the error says it is on line 1 in your file, I'd say its whitespace.

查看更多
相关推荐>>
4楼-- · 2019-09-19 19:41

Check your page encoding. Probably it UTF-8+ (with UTF-8 signature)

If not then you need to check white spaces.

查看更多
登录 后发表回答