Reply to mail, CRLF, RFCs, MTAs, Windows and Unix [long]

Your name:

Reply:


Posted by Christophe Chisogne on 02/17/05 18:54

I dont know if it's the right place to post, but anyway...
(Sorry for the cross-post.)

I think the doc for the mail function [1] should be improved,
after reading bug 15841 [2], mail.c [3], skim some RFCs,
and after I had different problems with PHP mail()

Basicaly, the doc says to use
- \n in body
- \r\n in extra headers

As far as I understand the matter:

- SMTP requires \r\n : cfr RFCs (ex 822, 2822)

- Under windows, PHP mail directly uses socket and SMTP

- Under *nix, PHP mail uses \n to send subject, to, etc [3]
to the sendmail/postfix/qmail binary (ok, *nix eol is \n),
then the MTA uses translate this to SMTP with \r\n
(adding \r to standalone \n if needed) -- ok, RFC want \r\n

Which basicaly means

- Under windows (SMTP, so \r\n)
use \r\n for body (doc says \n) : doc KO
use \r\n for mail headers (doc says \r\n) : doc ok

- Under *nix (local sendmail and eol, so \n, not SMTP)
use \n for body (doc says \n) : doc ok
use \n for mail headers (doc says \r\n) : doc KO

So, the doc is sometimes correct, sometimes not,
and it leads to problems (portability, ignored headers,
etc).

I think correct behaviour is

- windows (direct SMTP): use \r\n for headers and body
- *nix (local eol, then SMTP): use \n for headers and body

At least, it seems Postfix works that way, perhaps Qmail too
(I dont know for other MTAs)

My question is twofold (three?)

1. Am I right about that 'correct behaviour' or do I miss something?

2. If I am, could the doc be improved and explain that?

Ex doc says
"Some poor quality Unix mail transfer agents replace LF by CRLF"
but
\n is unix end of line, PHP src code use \n itself [3],
and MTA must speak SMTP and use \r\n, so conversion seems required.

-- I guess 3 is not a very good idea

3. use \n everywhere, php will automagically s#\n#\r\n# on windows only.
this would add portability between *nix and windows

Christophe

PS Below is an example of problem I had.

If I follow current mail() doc on my Linux server,
ie using \n for body (ok) and \r\n for headers (ko),
something like this will fail:

$headers = "From: a@b.com\r\nCc: c@d.com\r\rX-Mailer=efg";
$body = "Hello\nWorld";

Postfix (which uses \n for local *nix delivery [4])
generates headers with \n for standard headers
and \r\n for extra headers, which break RFCs
and cause many mail clients to see part of the headers
as part of the body (really ugly):

To: h@j.com\n
Subject: foo bar\n
From: a@b.com\r\n
Cc: c@d.com\r\n
X-Mailer: efg\r\n

Which some mail clients (thunderbird and others) see as

To: h@j.com\r\n
Subject: foo bar\r\n
From: a@b.com\r\r\n <-- headers break here
Cc: c@d.com\r\r\n
X-Mailer: efg\r\r\n


[1] PHP manual : mail()
http://www.php.net/manual/en/function.mail.php

[2] PHP Bug 15841
http://bugs.php.net/bug.php?id=15841

[3] PHP src : mail.c
http://cvs.php.net/php-src/ext/standard/mail.c

[4] Subject: Re: CRLF vs. LF (From: Wietse Venema)
http://archives.neohapsis.com/archives/postfix/2000-02/0398.html

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация