|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-03-02 20:05 UTC] rha at juggernaut dot com dot au
Last November the mail documentation was changed from saying: "Multiple extra headers are separated with a newline." to: "Multiple extra headers are separated with a carriage return and newline. Note: You must use \r\n to seperate headers, although some Unix mail transfer agents may work with just a single newline (\n)." This change is inaccurate. Line breaks in headers should be the native line endings for the system on which PHP is running. The mail() function is not talking to an SMTP server, so RFC2822 does not apply here. mail() is talking to a command line program on the local system, and it is reasonable to expect that program to require system-native line breaks. Use of CRLF is known to break qmail systems where no conversion of line breaks occurs on the input data. In this case using CRLF causes all but the first extra header to appear in the message body (CRLF is interpreted as two line breaks). Possibly the best resolution to this problem would be for the mail() function to convert any line breaks in arg 4 into the system's native line breaks. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 07:00:02 2025 UTC |
Documentation of mail() states: "Note: You must use \r\n to separate headers, although some Unix mail transfer agents may work with just a single newline (\n)." This is WRONG. On Unix systems, any line ending (also between header fields) has to be \n. On Windows Systems, it has to be \r\n. Here is part of the sounce code in mail.c (in case of a Unix system): sendmail = popen(sendmail_cmd, "w"); fprintf(sendmail, "To: %s\n", to); fprintf(sendmail, "Subject: %s\n", subject); if (headers != NULL) { fprintf(sendmail, "%s\n", headers); } fprintf(sendmail, "\n%s\n", message); ret = pclose(sendmail); On Windows Systems, the SMTP session is handled within SendText() in sendmail.c, which sends headers and message as is, so the line endings have to be \r\n. Please correct the above cited note and any samples. There is a further bug in example 4: | $headers .= "Bcc: birthdaycheck@example.com\r\n"; remove the ending line break The header must not end with a line break. As you can see in above code snipet, the mail implementation adds a \n at the end of the header. BTW, this is a documentation problem.I discovered another temporary solution. It's similar to @guy's but it is not using additional scripts that have to be installed (unix2dos,dos2unix). Also php didn't allow me to use piped sendmail in sendmail_path (php.ini) I'm using qmail. --- cd /var/qmail/bin cat > sendmailfix #!/bin/sh sed 's/^M$//' | /var/qmail/bin/sendmail ${1+"$@"} [Ctrl+D] chmod 555 sendmailfix chgrp popuser sendmailfix --- ^M is a combination of [Ctrl+V, Ctrl+M] Finally set the value of sendmail_path in php.ini to our script --- sendmail_path = /var/qmail/bin/sendmailfix -t -iThis Code: mail('nobody@example.com','test','Message Body',"From: me@example.com\r\nContent-Type: text/plain"); passes this to sendmail: To: nobody@example.com\n Subject: test\n From: me@example.com\r\n Content-Type: text/plain\n \n Message Body\n (on a Ubuntu 10.04 installation from ubuntu repository) So PHP uses \n to terminate the headers it creates on its own. It makes no sense to terminate additional headers with \r\n.