Skip to content

mail() throws TypeError after iterating over $additional_headers array by reference #10990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ras52 opened this issue Apr 1, 2023 · 1 comment

Comments

@ras52
Copy link

ras52 commented Apr 1, 2023

Description

The following code:

<?php
$headers = ['From' => '[email protected]'];
foreach ($headers as $n => &$v) ;
mail('[email protected]', 'Test', 'Test', $headers);

... resulted in this output in PHP 8.x:

PHP Fatal error:  Uncaught TypeError: Header "From" must be of type array|string, string given in /home/richard/test.php:4
Stack trace:
#0 /home/richard/test.php(4): mail()
#1 {main}
  thrown in /home/richard/test.php on line 4

I can't see how this can be anything other than a bug.

(The message is worded slightly different in 7.x, but clearly the same issue. It also proceeds to send the message but with no additional headers at all. I appreciated PHP 7.x is unsupported – I include this for information only. In PHP 8.x, the message is not sent at all.)

Comment out the iteration and it works (i.e. produces no output). It also works if you replace the iteration with one by value, i.e.

foreach ($headers as $n => $v) ;

Adding print_r($headers) before and after the iteration show, as expected, that the array has not changed.

Attempted workarounds like calling reset($headers) (in case it's some weirdness with internal array pointers), doing $v = (string)$v in the loop, or copying the array, either directly or indirectly with $headers = array_merge([], $headers); do not help. Workarounds that entirely avoid iteration by reference, e.g. by building up a second array, do work.

PHP Version

PHP 8.1.13, 8.0.26, 7.4.33 and 7.3.31

Operating System

Debian 11

@nielsdos
Copy link
Member

nielsdos commented Apr 1, 2023

This does indeed feel like a bug. The reason it breaks is that the foreach loop converts the value to a reference, and it looks like this isn't dealt with in mail().

nielsdos added a commit to nielsdos/php-src that referenced this issue Apr 1, 2023
…onal_headers array by reference

We should dereference the values, otherwise references don't work.
nielsdos added a commit that referenced this issue Apr 1, 2023
* PHP-8.1:
  Fix GH-10990: mail() throws TypeError after iterating over $additional_headers array by reference
  Fix GH-8841: php-cli core dump calling a badly formed function
nielsdos added a commit that referenced this issue Apr 1, 2023
* PHP-8.2:
  Fix GH-10983: State-dependant segfault in ReflectionObject::getProperties
  Fix GH-10990: mail() throws TypeError after iterating over $additional_headers array by reference
  Fix GH-8841: php-cli core dump calling a badly formed function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
@ras52 @nielsdos and others