zbateson / mail-mime-parser

An email parser written in PHP
https://mail-mime-parser.org/
BSD 2-Clause "Simplified" License
442 stars 56 forks source link

Cannot write attachment to disk #150

Open zrubinrattet opened 3 years ago

zrubinrattet commented 3 years ago

I've used the following code to parse a raw email:

use ZBateson\MailMimeParser\Message;

$message = Message::from($_POST['rawEmail']);

foreach ($message->getAllAttachmentParts() as $ind => $part) {
    $filename = $part->getHeaderParameter(
        'Content-Type',
        'name',
        $part->getHeaderParameter(
             'Content-Disposition',
             'filename',
             '__unknown_file_name_' . $ind
        )
    );
    file_put_contents($filename, $part->getContent());
}

But it only writes some of the attachment data to the file, not all of it. This works on my localhost but it does not work on my server (bluehost shared hosting).

I've looked at the examples on the library's website and tried a number of other ways to write the file besides file_put_contents($filename, $part->getContent());. Such as:

$out = fopen($filename, 'w+');
$str = $part->getBinaryContentResourceHandle();
$contents = stream_get_contents($str);
fwrite($out, $contents);
fclose($str);
fclose($out);

and

$out = fopen($filename, 'w+');
$str = $part->getBinaryContentResourceHandle();
stream_copy_to_stream($str, $out);
fclose($str);
fclose($out);

and

$stream = $part->getContentStream();
$part->saveContent($filename);

But none write all of the data to a file on the server. Any help appreciated! Thanks in advance!

zbateson commented 3 years ago

Hi @zrubinrattet

Unfortunately since it's only happening on your server it sounds likely to be a server issue, and not related to my library, and I have no idea what it could be. Maybe bluehost's support or forums or something can help?

FYI, my preferred version is the last one... 'saveContent' :) the two before it are effectively the same thing (except saveContent uses psr7 streams). I wouldn't use the 'getContent' version for saving an attachment, because if it has a charset defined it will try to convert that, which can cause issues: #103