0

I am struggling with sending emails from PHP script. When the mail() function is fired, AND the recipient has his mailbox hosted by a specific service (seznam.cz) AND the mail is downloaded into a mail client (Mozilla Thunderbird) AND ESET antivirus checks it, the message appears broken.

I think, that the problem is caused by antivirus inserting special header to the mail message and leaving empty line after that:

...
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 begins with the line X-PHP-Originating-Script. The rest of message includes all the HTML tags.

This is the script I used 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 Laravel framework, the emails are sent and displayed correctly. I compared the differences and realized, that the X-PHP-Originating-Script header is not sent by Laravel.

Could that be the reason? And how do I fix it?

Fi Lip
  • 30
  • 4
  • have you tried using PHPMailer to send the emails instead of building all the headers etc by hand? It usually results in a more reliable experience. – ADyson Apr 04 '23 at 15:37
  • @ADyson yes, I have used it before, I am just curious if someone knows what's wrong with this simple code... – Fi Lip Apr 04 '23 at 15:47
  • I wouldn't be surprised if `script.php` in a header is triggering an overly sensitive AV. Sounds like you can [pretty easily turn that header off](https://stackoverflow.com/a/25130535/231316). – Chris Haas Apr 04 '23 at 15:52
  • @FiLip you should test if *$emailbody* is a well-formed HTML document – Pippo Apr 04 '23 at 15:53

1 Answers1

0

It may just be a problem of different line separators.

Until PHP 7.4 the separator after the X-PHP-Originating-Script was just \n (this was changed in PHP 8 to use \r\n and even more recently in master to base the decision on other factors).

Since all your other headers are instead concatenated using \r\n, the antivirus may be doing some confusion here in adding the header. This results in a double line break that the client interprets as the begin of the body.

You'll probably better understand it looking at the raw message with an editor that shows all characters, including \n and \r.

The solution would be to match the line ending of your PHP version, or to remove the X-PHP-Originating-Script header at all setting mail.add_x_header = Off in php.ini.

AleRinaldi
  • 415
  • 4
  • 11