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 <somebody@example.com>',
'Content-Type: text/html; charset="UTF-8"',
];
mail('foo@example.com', 'Subject', 'Body', implode("\n", $headers));
A message that came in before the upgrade looked like this:
To: foo@example.com
Subject: Subject
From: Somebody <somebody@example.com>
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: somebody@example.com
X-Google-Original-From: Somebody <somebody@example.com>
Content-Type: text/html; charset="UTF-8"
To: somebody@example.com
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.
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: somebody@example.com
X-Google-Original-From: Somebody <somebody@example.com>
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.