PHP mail sending corrupted headers
P粉979586159
P粉979586159 2023-09-07 10:43:50
0
1
559

I'm working on sending an email from a PHP script. When themail()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 lineX-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 = '  ... '; $emailAltBody = "..."; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= 'From: ' . "\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 theX-PHP-Originating-Scriptheader is not sent by Laravel.

Could this be the reason? How can I solve it?

P粉979586159
P粉979586159

reply all (1)
P粉573943755

This may just be a problem of different line separators.

Until PHP 7.4the delimiter after X-PHP-Originating-Script was just\n(this has changedin PHP 8using\r\n, and even more recentlyin mastermaking 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\nand\r, and you may understand it better.

The solution is to match the line endings of the PHP version, or remove theX-PHP-Originating-Script时删除header at all settingsmail.add_x_header = Off in php.ini middle.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!