Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write attachment to disk #150

Open
zrubinrattet opened this issue Nov 17, 2020 · 1 comment
Open

Cannot write attachment to disk #150

zrubinrattet opened this issue Nov 17, 2020 · 1 comment

Comments

@zrubinrattet
Copy link

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants