Skip to content

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

Closed
@hansherlighed

Description

@hansherlighed

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions