php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Mail() function not working correctly in PHP 8.x

Open hansherlighed opened this issue 4 years ago • 56 comments

Description

The following code:

<?php
mail("[email protected]", "This is the subject", "This is the body");
?>

Resulted in this output of the DATA part of the communication from tcpdump on the receiving SMTP server when building container "FROM php:8.0.14-fpm-alpine" or "FROM php:fpm-alpine":

Received: by f3de6efeb3a0 (sSMTP sendmail emulation); Sat, 12 Feb 2022 06:28:10 +0100\r\n
From: "Linux User" <www-data@f3de6efeb3a0>\r\n
Date: Sat, 12 Feb 2022 06:28:10 +0100\r\n
To: [email protected]\r
\r\n
Subject: This is the subject\r
\r\n
\r
\r\n
This is the Body\r
\r\n
\r\n

And this is the resulting mail headers of the mail on the receiving SMTP server:

Return-Path: <www-data@f3de6efeb3a0>
X-Envelope-To: [email protected]
Message-ID: <[email protected]>
Received: from localhost ([127.0.0.1])
	by mail.example.com with SMTP
	for [email protected];
	Sat, 12 Feb 2022 06:28:10 +0100
Received: by f3de6efeb3a0 (sSMTP sendmail emulation); Sat, 12 Feb 2022 06:28:10 +0100
From: "Linux User" <www-data@f3de6efeb3a0>
Date: Sat, 12 Feb 2022 06:28:10 +0100
To: [email protected]

This is the output using the exact same environment, just that the container is build using "FROM php:7.4-fpm-alpine":

Received: by ae23725475da (sSMTP sendmail emulation); Sat, 12 Feb 2022 06:27:33 +0100\r\n
From: "Linux User" <www-data@ae23725475da>\r\n
Date: Sat, 12 Feb 2022 06:27:33 +0100\r\n
To: [email protected]\r\n
Subject: This is the subject\r\n
\r\n
This is the Body\r\n

And this is the resulting mail headers of the mail on the receiving SMTP server:

Return-Path: <www-data@ae23725475da>
X-Envelope-To: [email protected]
Message-ID: <[email protected]>
Received: from localhost ([127.0.0.1])
	by mail.example.com with SMTP
	for [email protected];
	Sat, 12 Feb 2022 06:27:33 +0100
Received: by ae23725475da (sSMTP sendmail emulation); Sat, 12 Feb 2022 06:27:33 +0100
From: "Linux User" <www-data@ae23725475da>
Date: Sat, 12 Feb 2022 06:27:33 +0100
To: [email protected]
Subject: This is the subject

The result is that when using PHP 8 there are some invalid "\r" which makes the mail format invalid and so the headers are not processed correctly on the receiving SMTP server. The result is that the email does not have a subject. I've also tried setting additional headers, but they are all invalid and not processed due to the invalid "\r". I've tried using both sendmail and ssmtp in the container. It yields the same result.

My php.ini setting are:

sendmail_path = '/usr/sbin/sendmail -t -i -S myserver:25'

Something has changed in the PHP mail() function between PHP 7 and PHP 8

PHP Version

PHP 8.0.14 and newer

Operating System

Docker php:8.0.14-fpm-alpine and php:fpm-alpine

hansherlighed avatar Feb 12 '22 07:02 hansherlighed

mail() now outputs proper email line endings (i.e. CRLF instead of LF). It looks like your MTAs convert that to CRCRLF, what would be a bug in those MTAs.

cmb69 avatar Feb 13 '22 09:02 cmb69

mail() now outputs proper email line endings (i.e. CRLF instead of LF). It looks like your MTAs convert that to CRCRLF, what would be a bug in those MTAs.

The capture from tcpdump is taken on the PHP server, so the trace is captured when going FROM PHP to the MTA(it's the output from sendmail). So the CRCRLF seems to comes from PHP, not the MTA(unless you refer to sendmail as the MTA). If I read the specifications correctly, PHP should output CRLF directly after each line(which it does in PHP 7.4), but does not in PHP 8.

When you say MTA, do you mean sendmail? Do you mean that it is sendmail that is converting the input from PHP to CRCRLF? Is there anyway to see the raw output sent from PHP to sendmail?

hansherlighed avatar Feb 13 '22 11:02 hansherlighed

You can see the raw PHP output by e.g. writing to a file instead of using sendmail. I.e. set sendmail_path to something like tee mailBasic.out >/dev/null.

cmb69 avatar Feb 13 '22 13:02 cmb69

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: [email protected]^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: [email protected]$
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. 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([email protected], "This is the subject", "This is the Body", "From: [email protected]\nContent-Type: text/plain"); results in:

cat -e raw_mailoutput_php8.out

To: [email protected]^M$
Subject: This is the subject^M$
From: [email protected]$
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.

The solution when using the "php:fpm-alpine" image is to set "sendmail_path" to: '/usr/bin/dos2unix -u|/usr/sbin/sendmail -t -i'

hansherlighed avatar Feb 13 '22 18:02 hansherlighed

The essence of the problem is that a UNIX command/program expects line endings to be "\n" and not "\r\n", […]

That is irrelevant. The respective RFCs are Internet standards, and those mandate CRLF as line break, so MTAs should be able to deal with that regardless of the operating system they're running on. Consider to report this issue to the sendmail maintainers (if it still hasn't been fixed in the latest version).

cmb69 avatar Feb 14 '22 10:02 cmb69

I stumbled upon the same issue recently.

The solution with dos2unix by @hansherlighed fixed it for me, thank you!

This issue happens both with busybox's and ssmtp's sendmail except with busybox's (unless you add verbosity) you will get this weird error sendmail: . failed when in fact it's the same RFC 2822 compliance error.

I don't know which SMTP server the OP used but I used OpenSMTPD, so possibly related https://github.com/OpenSMTPD/OpenSMTPD/issues/1135

csandanov avatar Apr 04 '22 00:04 csandanov

I just faced this issue in the PHP8.1 alpine image. It was fine in PHP7 images.

speller avatar Aug 29 '22 05:08 speller

Consider to report this issue to the sendmail maintainers

Why PHP contributors didn't do this knowing that alpine builds are failing one of the basic functions?

speller avatar Aug 29 '22 05:08 speller

Why PHP contributors didn't do this knowing that alpine builds are failing one of the basic functions?

Because it is not a PHP issue. mail() creates mails with CRLF line endings according to the standard; MTAs need to deal with this.

cmb69 avatar Aug 29 '22 09:08 cmb69

I have just been bitten by this on a site hosted by a commercial ISP, where I don't have root access, don't know precisely what distro is being used, and dos2unix is not installed, so I can't use the workaround. I'm afraid that I don't agree that this isn't a PHP issue. Previous PHP versions implemented mail() by communicating directly with the MTA. In doing so, they presumably correctly followed the RFCs requiring "\r\n" as the end of line characters. For your own reasons (and I suspect that they were quite good ones), you have now decided to use sendmail as an intermediary. sendmail is designed and intended to work at and from the Unix command line. It is therefore entirely reasonable for sendmail to expect header and other 'lines' to be terminated by the standard Unix "\n". If PHP chooses to use sendmail as its intermediary in sending mail, it behoves PHP to comply with its formatting rules.

pfletch101 avatar Nov 08 '22 20:11 pfletch101

PHP uses a configurable MTA (e.g. sendmail) on non Windows platforms for ages; nothing has changed in this regard. What has changed, is that as of PHP 8.0.0, the mail() function produces standard conforming header line endings throughout (i.e. CRLF, instead of LF). The first standard RFC which specifies these line endings is RFC 822 from August 13, 1982. If there are any MTAs which still cannot deal with that standard, I'd blame them. And whether these MTAs run on Linux or any other platform is irrelevant; the use of CRLF is mandated for all platforms.

dos2unix is not installed, so I can't use the workaround

You don't even need dos2unix for a workaround; something like sed /\r\n/\n/g should work as well, if you're allowed to change the sendmail_path setting.

cmb69 avatar Nov 08 '22 22:11 cmb69

@pfletch101 Nobody cares. PHP team has implemented a standard breaking the whole functionality and just saying "That's not our business to solve this". The Docker team who build the Docker image says "consider installing an alternative image" and closed the ticket. The sendmail bugtracker is dead and nobody responded at all: https://github.com/mirror/busybox/issues/58. In the end, we have a broken functionality, and everyone just ping-ponging users.

speller avatar Nov 09 '22 03:11 speller

@cmb69

PHP uses a configurable MTA (e.g. sendmail) on non Windows platforms for ages

Could you stop using sendmail since it doesn't follow the standard and:

I'd blame them

?

Does it look nonsense to blame an MTA for non-standard behavior and keep using it?

I think the PHP team should either fix the issue and revert back the compatibility with the tool that it "uses for ages" or stop using it and provide an alternative or make sure the team of the "tool used for ages" is aware of the changed protocol and release a version compatible with the updated PHP version. I can't see other options that keep the product quality high and do not break the basic functionality. Do you?

speller avatar Nov 09 '22 03:11 speller

Does it look nonsense to blame an MTA for non-standard behavior and keep using it?

PHP, despite the name of the INI setting, does not rely on sendmail (and never did). sendmail_path is just a command line to which the mail (as text) is piped, and which is then executed. You could even use some script (possibly written in PHP), which processes STDIN and "forwards" the mail to an SMTP server.

Note that this behavioral change is actually a bugfix (that bug affected other MTAs), namely that we sent mixed line endings (CRLF and LF), and that bug has finally been resolved, but we deliberately did that only for a major PHP version, because we were aware of the potential BC break.

Also note that it should be possible to allow the direct SMTP transfer, which is currently available only on Windows, on other platforms as well. A PR would be welcome.

cmb69 avatar Nov 09 '22 10:11 cmb69

The ability to send email is a fairly important functionality for a programming language whose primary purpose is to code website behaviors. It would seem that a choice to implement this with the use by means of an external intermediary (rather than using SMTP) should require that a reasonable effort be made to ensure that the implementation works and continues to work on most targeted systems. If you know that your chosen implementation has flaws and/or is not standards-compliant, the appropriate response would seem to be to change your implementation, not to expect the external intermediary to change its longstanding behavior, and certainly not to change your implementation in a way that will break functionality when the external intermediary predictably does not do so.

To say that the mail() function "does not rely on sendmail" may be technically true, for the reasons given, but I doubt whether more than 5% of the users of PHP know the details of how mail() works, nor should they need to! Only a very small minority would have the knowledge or ability to write (in essence) a replacement for sendmail.

pfletch101 avatar Nov 09 '22 14:11 pfletch101

@cmb69

does not rely on sendmail (and never did)

This is not true. sendmail is used as the default MTA here and there and everywhere across many files in PHP sources.

So the situation is completely weird. The PHP team uses sendmail actively as the default MTA, intentionally breaks the compatibility with this MTA, and shifts the responsibility to users blaming MTAs that they don't follow standards. Moreover, the PHP team has broke the compatibility and didn't suggest any easy-to-use replacement.

The only issue is that users don't f**** care about standards, they need a working solution that brings results. And the PHP team broke this functionality. MTAs didn't change their behavior, they're not a part of this bug. So this is clearly the PHP responsibility to fix it back.

I think a proper solution will be to add a configuration key to switch between two types of line endings. As easy as that.

speller avatar Nov 15 '22 02:11 speller

Stumbled over this one today after upgrading to 8.x. The stubbornness of the PHP maintainers is astonishing. It should not have surprised me though, after all the pain they caused me and others by making a product with a life cycle of just 3 years.

michaelbrunnbauer avatar Dec 19 '22 11:12 michaelbrunnbauer

I think this needs to be re-opened as we should not introduce a change that breaks compatibility with non conforming clients without an option to switch it back. I think it is a good thing to have conforming client by default but we should probably have some ini option to use non conforming behavior.

bukka avatar Dec 30 '22 12:12 bukka

I changed it to Feature because technically this is part of UPGRADING and breaking change in the major version so we cannot consider it as a bug - it will also require introducing a new INI. If the patch looks self contained, we might potentially be able to get it to 8.2.

bukka avatar Dec 30 '22 12:12 bukka

@cmb69 is it just this change? https://github.com/php/php-src/commit/6983ae7 . If so, it should be pretty easy to make it configurable...

bukka avatar Dec 30 '22 12:12 bukka