Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
@ras52

Description

@ras52

Description

The following code:

<?php$headers = ['From' =>'test@example.com'];foreach ($headersas$n => &$v) ;mail('test2@example.com','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:4Stack 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 ($headersas$n =>$v) ;

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

Attempted workarounds like callingreset($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

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


      [8]ページ先頭

      ©2009-2025 Movatter.jp