Home > other >  PHP8 mail function adding space prefixes to headers
PHP8 mail function adding space prefixes to headers

Time:12-28

I just upgraded from PHP 7.4 to 8.0. All of the sudden outgoing mails are getting spaces prefixed to the custom headers.

Here's the code:

$headers = [
    'From: Somebody <[email protected]>',
    'Content-Type: text/html; charset="UTF-8"',
];
mail('[email protected]', 'Subject', 'Body', implode("\n", $headers));

A message that came in before the upgrade looked like this:

To: [email protected]
Subject: Subject
From: Somebody <[email protected]>
Content-Type: text/html; charset="UTF-8"
Message-Id: <E1n1vlx-0002C5-FC@hostname>

Body goes here

After the upgrade, the same code sends a message which looks like this for the receiver:

From: [email protected]
X-Google-Original-From: Somebody <[email protected]>
 Content-Type: text/html; charset="UTF-8"
To: [email protected]
Subject: Subject
Message-Id: <E1n20bv-0007st-Eh@hostnme>

Body goes here

The Content-Type header is getting a space added to the beginning and thus mail clients are ignoring it. exim4 doesn't have any logs of the full outgoing message so I'm not sure where to turn next.

CodePudding user response:

Use \r\n instead of \n for your implode -- RFC5321 explicitly specifies CRLF as the line terminator. I notice that Gmail is adding this X-Google-Original-From header, between the two header lines that you specified:

From: [email protected]
X-Google-Original-From: Somebody <[email protected]>
 Content-Type: text/html; charset="UTF-8"

I'll wager that the space is an artifact of Google trying to insert this line between two of yours, but not really two of yours, because incorrect line endings.

CodePudding user response:

I think the $headers shouldn't be an array, just use simple string. you can see the example #2 here https://www.php.net/manual/en/function.mail.php

  • Related