I'm working on sending an email from a PHP script. When the mail()
function is triggered, the recipient's mailbox is hosted by a specific service (seznam.cz) and the message is downloaded to the mail client (Mozilla Thunderbird) and ESET antivirus software Checking, the message appears to be corrupted.
I believe that the problem is caused by antivirus software inserting special headers in mail messages and leaving empty lines after them:
... Subject: =?UTF-8?B?Tm92w70gxI1sZW4gd2VidSBBU1AgxIxSIQ==?= X-EsetId: 37303A298C7FEE69657363 X-PHP-Originating-Script: 80:script.php MIME-Version: 1.0 Content-type:text/html;charset=UTF-8 ...
My email client thinks the message is plain text and starts with the line X-PHP-Originating-Script
. The remainder of the message includes all HTML markup.
This is the script I use to send the email:
$subject = mb_encode_mimeheader('Subject text'); $emailBody = '<!DOCTYPE html> <html lang="cs"> ... </html>'; $emailAltBody = "..."; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: <email.address@example.com>' . "\r\n"; $result = mail("email.address@example.com", $subject, $emailBody, $headers);
However, when using the Laravel framework, the email is sent and displayed correctly. I compared the differences and realized that the X-PHP-Originating-Script
header is not sent by Laravel.
Could this be the reason? How can I solve it?
P粉5739437552023-09-08 00:38:50
This may just be a problem of different line separators.
Until PHP 7.4 the delimiter after X-PHP-Originating-Script was just \n
(this has changed in PHP 8 using \r\n
, and even more recently in master making decisions based on other factors).
Since all other headers are concatenated using \r\n
, antivirus software may cause some confusion when adding headers. This results in a double newline, which is interpreted by the client as the start of the body.
View the original message with an editor that displays all characters, including \n
and \r
, and you may understand it better.
The solution is to match the line endings of the PHP version, or remove the X-PHP-Originating-Script
header at all settings mail.add_x_header = Off in php.ini middle.