Jacob Richman
Jacob Richman

Reputation: 471

How do I save an inline image in an email using piping with PHP?

I have a server with email piping set up. I was able to save email attachments using this, but when I send a picture from my phone, for example, it won't save because the picture is "inline" instead of as an attachment. Is there a way to save the inline picture?

Upvotes: 0

Views: 179

Answers (2)

Jacob Richman
Jacob Richman

Reputation: 471

This is the full answer based off of exussum's response:

    //create the array of base64 strings
    $pieces = explode("Content-Transfer-Encoding: base64", $email);
    array_shift($pieces);
    foreach ($pieces as &$value) {
        $newString = strstr($value, "\n\n");
        $newString = substr($newString, 0, strpos($newString, '--'));
        $PicturesData[] = $newString;
    }

    //save each image
    foreach ($PicturesData as &$value) {
        $name = time() . ".png";
        while(file_exists("pics/" . $name)) {
            $name = time() . ".png";
        }
        file_put_contents("directory/".$name, base64_decode($value));
    }

This will create an array of the images embedded in an email and save each image as a different name.

Upvotes: 0

exussum
exussum

Reputation: 18550

an inline image appears like this in the source of an email

--------------090909040108020409080705
Content-Type: image/png;
 name="fideghfb.png"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
Content-Disposition: inline;
 filename="fideghfb.png"

BASE64
--------------090909040108020409080705--

Simply take the base 64 from that, your looking for a block with Content-Disposition: inline; and then base64 decoding the image

Upvotes: 1

Related Questions