简体   繁体   中英

sending mail from php: headers are interpreted as body?

When I send mail from php with \\r\\n as line break in the headers ( as it should be according to documentation )

$headers = "From: $email\r\n"; 
$headers .= "Reply-To:  Just me <$email>\r\n"; 
$headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$subject = "Hello world";
$body = "<html><p>Hey, whats up?</p></html>";

mail($to, $subject, $body, $headers);

Some mail clients will interpret \\r\\n as being two line breaks. So for this mail() above the real mail content would look like this:

X-Message-Delivery: Vj0LEdMMtPAYT0xO0Q9MTtTQ0w9MA==
X-Message-Status: n
Received: from server75.publicompserver.de ([92.43.108.63]) by snt0-mc2-f13.Snt0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675);
 Thu, 9 Dec 2010 12:09:22 -0800
Message-ID: <40177C.70807@justme.org>
[lots of other headers]
Date: Thu, 09 Dec 2010 21:09:32 +0100
X-OriginalArrivalTime: 09 Dec 2010 20:09:22.0873 (UTC) FILETIME=[F88C3A90:01CB97DC]
From: $email

Reply-To:  Just me <$email>

Content-type: text/html; charset=iso-8859-1

Content-Transfer-Encoding: 8bit

<html><p>Hey, whats up?</p></html>

Now some clients (googlemail for example) will ignore these extra linebreaks. Others (thunderbird) will interpret the first extra linebreak as being the end of the headers and will interpret the rest of the header lines as being part of the body (losing header information, in this case rendering the mail as text instead of html).

I've seen the same problem from other mail-sending websites, too.

What is happening here? \\r\\n is the correct line break according to doc, or is something else going wrong here? And how can it be resolved? Changing the line break to \\n instead of \\r\\n seems to help, but since the docs say "thou should use \\r\\n" this can't be right, can it?

This is mentioned in the documentation at http://php.net/manual/en/function.mail.php : "If messages are not received, try using a LF (\\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822."

So, as you said: it's not right, but it's reality.

As mentioned in your previous question. Each platform delimits lines in a different way (CRLF or LF). It's best to leave this decision up to PHP via the PHP_EOL constant and PHP will take care of it.

In your code, you're using \\r\\n for some of the header lines, and only \\n for others. Maybe this problem wouldn't occur if you were consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM