我正在尝试从发送到我的邮件服务器的电子邮件中获取图像并将文件保存到服务器。我用以下代码尝试过:
$no_of_occurences = 0;
$intStatic = 2;
$decode = imap_fetchbody($mbox, $val , null);
$no_of_occurences = substr_count($decode,"Content-Transfer-Encoding: base64");
$no_of_occurences--;
echo $no_of_occurences;
if($no_of_occurences > 0){
for($i = 0; $i < $no_of_occurences; $i++){
$strChange = strval($intStatic+$i);
$decode = imap_fetchbody($mbox, $val , $strChange);
$data = base64_decode($decode);
$x = 1;
$fName = './images/'.$ticketurl."_".$x. '.png';
while(file_exists($fName)){
$x++;
$fName = './images/'.$ticketurl."_".$x. '.png';
}
if($x==500){
$valid = false;
exit;
}
$file = $fName;
$success = file_put_contents($file, $data);
}
}
此代码可以正常工作,但前提是您具有一种附件类型。如果将一幅图像内嵌在邮件中,则它可以正常工作。如果将附件作为附件放置,它也可以使用。但是,如果同时拥有(内联和附件),则只获得一张图像和第二个干净的png文件。 有什么建议可以解决此问题吗?