如何使用带有 PHP 的管道在电子邮件中保存内联图像?

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

我有一台设置了电子邮件管道的服务器。我能够使用 this 保存电子邮件附件,但是当我从我的 phone 发送图片时,它不会保存,因为图片是 "inline" 而不是作为附件.有没有办法保存内联图片?

内联图像在电子邮件的来源中显示为这样

--------------090909040108020409080705
Content-Type: image/png;
 name="fideghfb.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.05000606.01050306@server.com>
Content-Disposition: inline;
 filename="fideghfb.png"

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

简单地从中获取 base 64,你用 Content-Disposition: inline; 寻找一个块,然后 base64 解码图像

这是基于 exussum 回复的完整答案:

    //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));
    }

这将创建一个嵌入在电子邮件中的图像数组,并将每个图像保存为不同的名称。