Php 从电子邮件中提取图像

Php 从电子邮件中提取图像,php,imap,Php,Imap,我需要生成HTML文件与其他图像从电子邮件 所有数据都是通过php\u imap 电子邮件中的img标签 我得到了多个img标记,但是如何提取实际图像部分cid:part3.08050603。09060803@example.com从电子邮件中提取并保存为文件?获取图像源 您可以使用和来查询图像src $doc = new DOMDocument(); $doc->loadHTML($email); //load the string into DOMDocument $sele

我需要生成HTML文件与其他图像从电子邮件

所有数据都是通过
php\u imap

电子邮件中的img标签

我得到了多个
img
标记,但是如何提取实际图像部分
cid:part3.08050603。09060803@example.com
从电子邮件中提取并保存为文件?

获取图像源 您可以使用和来查询图像
src

$doc = new DOMDocument();
$doc->loadHTML($email); //load the string into DOMDocument   
$selector = new DOMXPath($doc); //create a new domxpath instance
$images = $selector->query('//img/@src'); //Query the image tag and get the src
foreach ($images as $item) {
   echo $item->value; //grab the value (output: cid:part3.08050603.09060803@example.com)
}

另存为文件 现在您已经获取了图像源,您可以将内容写入文件。这意味着您需要打开
allow\u url\u fopen

foreach ($images as $item) {
   $content = file_get_contents($item->value);
   file_put_contents('./'.str_replace("/", "-", ltrim(parse_url($item->value)['path'],'/')), $content );
}
foreach

从电子邮件中提取图像

我需要获取数据部分。。不是html标记:)或者如果您可以回答这两个问题。。那太好了。。但是主要的问题是获取图像数据并将数据保存为文件。HTML解析器如何知道cid:images在哪里?这些引用必须由查看器填写到实际附件。附件具有需要与之匹配的内容ID:标题。您无法仅从字符串中获取这样的文件数据。。您需要从imap资源中获取数据您需要解析MIME数据,您需要一个用于PHP的外部MIME解析器库。
foreach ($images as $item) {
   $content = file_get_contents($item->value);
   file_put_contents('./'.str_replace("/", "-", ltrim(parse_url($item->value)['path'],'/')), $content );
}