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
BTW why are people pointing to rfc822? The introduction seems very clear to me:
Some message systems may store messages in formats that differ from the one specified in this standard. This specification is intended strictly as a definition of what message content format is to be passed BETWEEN hosts.
Note: This standard is NOT intended to dictate the internal formats used by sites, the specific message system features that they are expected to support, or any of the characteristics of user interface programs that create or read messages.
BTW why are people pointing to rfc822? The introduction seems very clear to me:
Some message systems may store messages in formats that differ from the one specified in this standard. This specification is intended strictly as a definition of what message content format is to be passed BETWEEN hosts. Note: This standard is NOT intended to dictate the internal formats used by sites, the specific message system features that they are expected to support, or any of the characteristics of user interface programs that create or read messages.
I see the point here as technically it is up to MTA to follow the RFC when sending the message. This is really just a program interface. On the other side, the current format had its issue so an attempt to fix them was probably in place. I would personally treat this as an omission but I'm not sure if requiring compliance was in place here. I have to say that after doing some research I haven't found anywhere any spec for sendmail interface that would mandate message format to follow the RFC 822, 2822 or 5322. Honestly I found very little info in sendmail docs but I might have missed something so if anyone else sees it somewhere, please share your findings.
@bukka Thank you a lot for the fix!
@bukka Thank you for the fix! I was also recently bitten by this issue. Do you know when your GH-8086 fix will make it into the next PHP 8.1.* release? I was inspecting the most recent commits contained in 8.1.15 and this fix doesn't appear to be included: https://github.com/php/php-src/compare/PHP-8.1.13...php-8.1.15
@ElliotNB The fix is not in 8.1 as it's somewhere between feature and a bug and it adds a new ini which is cleaner to do in the last supported version. Also there's a work around so I would suggest to use that in 8.0 and 8.1:
The solution when using the "php:fpm-alpine" image is to set "sendmail_path" to: '/usr/bin/dos2unix -u|/usr/sbin/sendmail -t -i'
Dears, Is there any final class for sending emails to be used in PHP version greater than 8 without changing the sendmail_path ? Since we are not allowed to change the .ini file configuration in some shared hosting.
@aly9 no but you should contact the hosting provider to change it globally in their configs.
I'm experiencing this issue too and it's driving me insane , both on PHP 8,1 and 8,0, and i am not using alpine
https://github.com/Shardj/zf1-future/issues/274
@bukka Thanks for your reply, but I have modify my sending emails script to use PHPMailer Class and it is working fine.
Sendmail binaries on linux, and pretty much every other OS using LF only are designed to parse normal text for the operating system when piped to directly.
They internally convert to match the RFC after, which causes a doubled up CR usually with php 8.x now.
Actual Sendmail does have a -ba parameter, which does place it into ARPANET mode, which will do the right thing when presented with a text file containing CRLFs for injection. specifying the -ba option in sendmail path does fix the issue, but naturally, the email tester doesn't do this. So the email test fails.
qmail's fake sendmail binary does NOT SUPPORT -ba. not even the alternate usendmail wrapper does. postfix, and most other MTAs that provide a fake sendmail binary do not support ARPANET mode like a real sendmail binary does. The only one that i've seen that does this is actual sendmail itself.
The sendmail binary has always accepted email for injection in the OS native text format. All other MTAs that have fake sendmail binaries do the same. They all still send RFC compliant emails, doing the needed conversion. When telnetting or s_clienting to the smtp port, telnet itself usually handles this part, and will convert the line endings properly.
I'm sorry, but PHP is wrong here. it's mail() function expects either smtp on windows, or a sendmail binary on linux/bsd/osx/etc. And sendmail has ALWAYS accepted messages in the UNIX (linefeed only) text scheme on unix, and it always will.
This isn't a silly RFC issue. Sendmail is NOT breaking the RFC by accepting and interpreting piped data stored in native OS text format. the RFC is for message transmission, and when using sendmail binary, php is not transmitting itself. it's handing off to the sendmail binary.
Exim is one that's smart enough to just do the right thing. It actually just strips out the \rs and then puts them back when it send the message :). I'm sure there are some others, but as we can see, it's just not widespread enough.
Now this IS something that configure should be able to catch.
actual sendmail will respond to "sendmail -ba" with 550 Recipient names must be specified
in this case, it's real sendmail, and -ba should be added to the options automatically.
Any other response mean sit's not actual sendmail, we can't do that.
I think, PHP is mixing up responsibilities. There are 2 involved parties and both of them have their own tasks.
It is PHP's responsibility to assemble all information needed by the mail agent. And it is PHP's responsibility to hand that information over to the mail agent in way that is valid for the OS and the mail agent itself.
This is where PHP's responsibilities end.
The mail agent is responsible to take the information and create a Mail that complies to the respective RFCs.
We now have the situation that the standard way of sending mails is broken due to PHP's interference in something that is not their business.
I tried the new config variable mail.mixed_lf_and_crlf = On and at first this seemed to work as before. All mails look nice in Outlook an Thunderbird and everywhere. But I keep getting bounces from remote mail servers along the lines of
SMTP error from remote server for TEXT command, host: **.**.com (***.***.***.***) reason: 554 message body contains illegal bare CR/LF characters.
This is unacceptable. The only thing that seemed to work is undoing PHP's magic by setting
sendmail_path='/usr/bin/dos2unix -u|/usr/sbin/sendmail -t -i'
With this config setting, mails look good in all clients and there are no bounces. Seems like my mail agent (nullmailer) cares about RFC compliance quite fine on it's own.
May I ask you to revert this broken feature? Ensuring mailing RFCs is not PHP's responsibility.
mail.mixed_lf_and_crlf = on should be an exact revert of the breaking change. If you see some differences between what was in PHP 7 and what mail.mixed_lf_and_crlf = on does, please open a new issue (bug) with all the info and I will take a look.
The CORRECT behavior is to produce standard text in the operating system's standard text format when piping to sendmail, and to use CR+LF when using SMTP, which is the circumstance where having it be CRLF matters. Funny enough this setting is used on windows, which already uses CRLF.
PHP has a responsibility to pipe stuff to sendmail binaries in the format the sendmail binary expects, which is ALWAYS the native text format used by the operating system, unless it's real sendmail in ARPANET mode.
If you want any change to happen, you need to create a new issue. Comments in closed issues are pretty much useless because it just get forgotten.
I also had issues with mail.mixed_lf_and_crlf = On
couldn't make it work with msmtp, so I keep using /usr/bin/dos2unix
For the record I created a new feature request to address the above: https://github.com/php/php-src/issues/19188 . If you have any further suggestions, please comment there.