I use phpmail script to send attachments from site to e-mail. The script itself works. I noticed that e.g. in the case of Gmail messages are coming but without attachments. Instead, in addition to the content, there is a very long message containing only the hash string.

In addition, not all mailboxes display content correctly. In some cases, the content also adds, for ex.:

--PHP-alt-41babb7e7ab4c64e3af6ea533924bebdContent-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit

and the html coding in plain text.

Can it be standardized so as not to display unnecessary information?

Here's my code:

<?php
$to = $_POST['email'];
$subject = 'Attachment from site domain.com';
$subject = sprintf("=?utf-8?B?%s?=", base64_encode($subject));
$random_hash = md5(uniqid(time()));

$headers = "From: MySite <site@domain.com>\r\nReply-To: site@domain.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$filename = $_POST['file_id'] . ".pdf";
$path = "/home/ftp/wp-content/uploads";
$file = $path . "/" . $filename;

// read file into $data var
$f = fopen($file, "rb");
$data = fread($f, filesize( $file ) );
fclose($f);

$attachment = chunk_split(base64_encode($data));

ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

Hello,
Here the mail text.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 8bit

<h2>Hello,</h2>
<p>Here's the mail text</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Transfer-Encoding: base64
Content-Type: application/pdf; name="<?php echo $filename; ?>"
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
$message = ob_get_clean();
if (@mail($to, $subject, $message, $headers )) {
echo "<p><b>File send!</b></p> Check your e-mail.";
} else {
echo "<p><b>Error!</b></p> File not send.";
}
?>

What can I change to make delivery with an attachment work for all mailboxes?

    One thing you could consider doing is using an existing PHP mail composer (such as Mail_Mime but that is by no means the only one) rather than building it manually.
    One problem that your code seems to have is that it's missing the MIME-Version: 1.0 header to indicate that your email is MIME-encoded.

      I've always used PHPMailer for mail stuff -- not because I think it's the best, but because it's the first one I used, so I'm somewhat familiar with it. Any such option lets someone who's hopefully an expert on this messy stuff take care of all the down-in-the-weeds details. 🙂 For example, this example code from it's GitHub repo shows how you just use its addAttachment() method to attach a file.

        Thank you for your answers. I use this script in Wordpress, which is why I didn't want to migrate to other solutions. After small modifications I manage to send an email with an attachment. The only problem is that the attachment is corrupted. It cannot be opened even though it is a pdf and has its size.

          Doesn't Wordpress's own mail function already have provision for attachments? I don't know if it can do both plain and HTML bodies, but I'd be surprised if there wasn't a Wordpress plugin that does.

            I have this on a custom mail class that we have used and are probably still using in some cases:

            /* July 10, 2018. We found out yesterday that gMail users were
            seeing their mails in raw HTML (with tags) format.

            Among the changes made to resolve this issue (several hours'
            investigation/work): addition of two hyphens after the closing
            boundary separator, and use of HEREDOC syntax to replace manually
            added \r\n newlines which may have been a problem. This class
            seems to be serving the transaction email to gMail and others as
            we expect.
            */

              Write a Reply...