简体   繁体   English

从php发送邮件:标题被解释为正文?

[英]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 ) 当我从php发送带有\\ r \\ n的邮件作为标题中的换行符( 因为它应该根据文档

$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. 某些邮件客户端会将\\ r \\ n解释为两个换行符。 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. 现在一些客户(例如googlemail)会忽略这些额外的换行符。 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). 其他人(thunderbird)会将第一个额外的换行符解释为标题的结尾,并将其余的标题行解释为正文的一部分(丢失标题信息,在这种情况下将邮件呈现为文本而不是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? \\ r \\ n根据doc是正确的换行符,还是其他出错的地方? 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? 将换行符更改为\\ n而不是\\ r \\ n似乎有所帮助,但由于文档说“你应该使用\\ r \\ n”这不可能是正确的,可以吗?

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." 这在http://php.net/manual/en/function.mail.php的文档中提到:“如果没有收到消息,请尝试仅使用LF(\\ n)。一些劣质的Unix邮件传输代理取代由CRLF自动填充LF(如果使用CRLF,则会导致CR加倍)。这应该是最后的手段,因为它不符合»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). 每个平台以不同的方式(CRLF或LF)界定线条。 It's best to leave this decision up to PHP via the PHP_EOL constant and PHP will take care of it. 最好通过PHP_EOL常量将此决定留给PHP,PHP将负责处理它。

In your code, you're using \\r\\n for some of the header lines, and only \\n for others. 在您的代码中,您对某些标题行使用\\ r \\ n,而对其他标题行仅使用\\ n。 Maybe this problem wouldn't occur if you were consistent. 如果你保持一致,也许不会出现这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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