[php-src] Issue #8086: Mail() function not working correctly in PHP 8.x
| From: | hansherlighed | Date: | Sun, 13 Feb 2022 18:40:37 +0000 |
| Subject: | [php-src] Issue #8086: Mail() function not working correctly in PHP 8.x | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239748@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8086
Comment Author: hansherlighed
Thanks @cmb69
I was able to get the raw output from the mail() function into a file using your advice.
You are indeed right, the raw output from PHP 8 looks like this:
```
cat -e raw_mailoutput_php8.out
To: redatced@example.com^M$
Subject: This is the subject^M$
^M$
This is the Body^M$
```
and in PHP 7 it looks like this:
```
cat -e raw_mailoutput_php7.out
To: redacted@example.com$
Subject: This is the subject$
$
This is the Body$
```
This issue has actually been reported to PHP way back in 2002:
https://bugs.php.net/bug.php?id=15841
The problem remains the same as reported in this bug. on UNIX systems, the line ending is expected
to be "\n" as it is the native line ending for the system. Since PHP's mail()
function on UNIX systems, is basically sending raw text output to a UNIX command(sendmail if you
follow the documentation) the mail() function will not work as sendmail expects the line endings to
be "\n" and NOT "\r\n".
By making this new change to the mail() function so it always uses "\r\n" for the build in
headers for Subject: and To:, you've actually made things worse, and made your documentation
invalid. It states in the mail() documentation page:
```
Note:
If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably
[» qmail](http://cr.yp.to/qmail.html)) 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://www.faqs.org/rfcs/rfc2822).
```
Following this note will cause the output from PHP8 to be a mix of "\r\n" and
"\n" for the headers. This will not work since the build in headers for "To:"
and "Subject:" will still use "\r\n" even if you set the additional headers to
use only "\n".
running
mail(redacted@example.com, "This is the subject", "This is the
Body", "From: me@example.com\nContent-Type: text/plain");` results in:
```
cat -e raw_mailoutput_php8.out
To: redacted@example.com^M$
Subject: This is the subject^M$
From: me@example.com$
Content-Type: text/plain^M$
^M$
This is the Body^M$
```
The essence of the problem is that a UNIX command/program expects line endings to be "\n"
and not "\r\n", and since the mail() function on UNIX systems, forwards/outputs to a UNIX
command rather than talking to an SMTP server directly, it causes problems.
Why can't the mail() function use the same line endings as PHP_EOL, so that on windows it
becomes "\r\n" and unix "\n"? That would make the function more platform
independent.