Search code examples
phpemailemail-attachmentspiping

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?


Solution

  • 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.