Mail() function not working correctly in PHP 8.x
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
Yeah, it is just that change. But do we really need to make that configurable? sendmail has a -ba option, and it looks like this would enable proper handling of CRLF.
Ah ok, I didn't know that there is an option for that as the only suggestion before was just to use dos2unix which didn't sound to me like an acceptable work around and I haven't checked the sendmail docs. But if setting sendmail_path to /usr/sbin/sendmail -ba -t -i fixes the issue, then there's no point to introduce extra ini.
I will close it but if -ba doesn't work for anyone, we can re-open and look to the optionally disabling the change.
Actually if that's the case we should set it here as well: https://github.com/php/php-src/blob/f40c3fca88bd3a7ed516f05ba04999dcb3256ab2/main/main.c#L669
Frankly, I haven't tried the option, since I'm usually working on Windows. Would be great if someone could actually try it out, and if it works, we should probably document it.
Yeah and also update it in https://github.com/php/php-src/blob/f40c3fca88bd3a7ed516f05ba04999dcb3256ab2/php.ini-production#L1098 and https://github.com/php/php-src/blob/f40c3fca88bd3a7ed516f05ba04999dcb3256ab2/php.ini-development#L1096
I will give it a try on the reported alpine image
I just ran some tests:
$ echo '<?php mail("[email protected]", "This is the subject", "This is the body");' > mailtest.php
$ docker run -v `pwd`:/var/www -w /var/www php:8 php -d sendmail_path="cat > ./test.8.log" mailtest.php
$ docker run -v `pwd`:/var/www -w /var/www php:7 php -d sendmail_path="cat > ./test.log" mailtest.php
$ hexdump -c test.8.log
0000000 T o : s o m e o n e @ e x a m
0000010 p l e . c o m \r \n S u b j e c t
0000020 : T h i s i s t h e s u
0000030 b j e c t \r \n \r \n T h i s i s
0000040 t h e b o d y \r \n
000004b
$ hexdump -c test.log
0000000 T o : s o m e o n e @ e x a m
0000010 p l e . c o m \n S u b j e c t :
0000020 T h i s i s t h e s u b
0000030 j e c t \n \n T h i s i s t h
0000040 e b o d y \n
0000047
The interesting part is that the PHP7 version uses \n to separate the Header lines, where PHP8 seems to use \r\n.
As no MTA whatsoever is involved in this test I assume we can ignore sendmail (or whatever people think is sendmail in their respective setup)
As the result from PHP8 is what is expected per RFC822 I assume we can say that the bug was fixed in general. Any issues with particular images need to be addressed in that image. Any issues with particular MTAs need to be addressed with that MTA. PHP is (after how many years now?) finally passing valid code towards whatever follows when calling mail
If anyone else can reproduce this or - even better - can not reproduce this, please post your findings here
The results when using php:8-alpine instead of php:8 are the same. It is using the correct RFC822 conform line-ending.
If anyone else can reproduce this or - even better - can not reproduce this, please post your findings here
There is even a test for this: https://github.com/php/php-src/blob/master/ext/standard/tests/mail/bug47983.phpt.
Not all sendmail variants have the -ba option, e.g. qmail, netqmail, s/qmail.
Not all sendmail variants have the -ba option, e.g. qmail, netqmail, s/qmail.
According to Wikipedia, qmail had it's final release 24 years ago; netqmail 15 years ago. I would presume that such mailers have other issues, which might render them irrelevant. s/qmail seems to be maintained, though. However, are these even affected by this CRLF issue? Or do they perhaps have another option to conform to the 40 year old specs?
s/qmail seems still maintained though so if it has got that issue and there's no -ba option, it's a good enough reason to introduce the switch.
I actually tested the sendmail v8.15.2 on Ubuntu 20.04 and sending email works fine there. It seems that just some variants might have got this issue.
The thing is that if those variants do not support -ba, we can't add it as a default.
I'm just reading the message sent above and it looks that the actual problem is with sSMTP which actually doesn't support -ba so it is not a solution by the look of it. See https://linux.die.net/man/8/ssmtp
So we probably need that INI that reverts it...
Not all sendmail variants have the -ba option, e.g. qmail, netqmail, s/qmail.
According to Wikipedia, qmail had it's final release 24 years ago; netqmail 15 years ago. I would presume that such mailers have other issues, which might render them irrelevant. s/qmail seems to be maintained, though. However, are these even affected by this CRLF issue? Or do they perhaps have another option to conform to the 40 year old specs?
They are all affected and they are all alive. And there are many more variants of sendmail I am not familiar with. Stuff is not useless just because it is old. One does not always need newer features - especially on systems relaying mail to a smarthost. But please go ahead to the future and leave the past behind. I'll be smiling while the past catches up with the maintainers many many times in the next years.
Stuff is not useless just because it is old.
Right. So why don't you just stick with PHP 7.4?
Stuff is not useless just because it is old.
Right. So why don't you just stick with PHP 7.4?
You have no idea. I actually lost customers who would move somewhere else because they can keep using PHP 7.4 there. Most of the others had to be forced to upgrade their stuff - which was not easy for many of them. I know people who still use PHP 5.3. Screaming all day to them what a security risk that is doesn't change a thing. You have no idea. No idea at all.
Not all sendmail variants have the -ba option, e.g. qmail, netqmail, s/qmail.
According to Wikipedia, qmail had it's final release 24 years ago; netqmail 15 years ago. I would presume that such mailers have other issues, which might render them irrelevant. s/qmail seems to be maintained, though. However, are these even affected by this CRLF issue? Or do they perhaps have another option to conform to the 40 year old specs?
They are all affected and they are all alive. And there are many more variants of sendmail I am not familiar with. Stuff is not useless just because it is old. One does not always need newer features - especially on systems relaying mail to a smarthost. But please go ahead to the future and leave the past behind. I'll be smiling while the past catches up with the maintainers many many times in the next years.
Well we sometime need to stop supporting of some old versions and clients because it will become very difficult to make sure that the stuff still works there. In general we tend to do that when the support ends on the distributions that are still supported. For example currently the oldest distribution that we still support is RHEL 7 and its variants that are still alive (CentOS 7, Amazon Linux 2). That's how we decide to drop specific versions of the libraries.
This particular case is not such a case as it impacts many still supported emulations. However this problem is also causing some issue for other MTA as described in https://bugs.php.net/bug.php?id=47983 and PHP 8.0 was a good time to fix it. It was however omission to not keep the compatibility optionally which we should do if such change is done. However it's difficult to know that when making the change because it is very hard to actively test all MTA's. The only thing we can do is to add that option to the future version and learn from this that doing changes to mail output should require more testing and consider more MTA's.
I just created a PR https://github.com/php/php-src/pull/10191 that introduces an option to revert back to the previous behavior.