如何在PHP中使用imap从电子邮件中提取内联图像(非附件)

如何在PHP中使用imap从电子邮件中提取内联图像(非附件),php,imap,Php,Imap,在php中使用imap获取电子邮件时,我会获取电子邮件正文内容,但无法提取粘贴在电子邮件正文中且未附加在其中的内联图像。在电子邮件内容正文中搜索图像 假设您的电子邮件正文是$body,那么您可以使用preg_match获取图像url。 使用此preg_match表达式获取图像源 preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $body, $matches); preg\u match('/]*src*=*[“\']”?([^

在php中使用imap获取电子邮件时,我会获取电子邮件正文内容,但无法提取粘贴在电子邮件正文中且未附加在其中的内联图像。

在电子邮件内容正文中搜索图像

假设您的电子邮件正文是
$body
,那么您可以使用preg_match获取图像url。 使用此preg_match表达式获取图像源

preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $body, $matches);
preg\u match('/<*img[^>]*src*=*[“\']”?([^“\']*)/i',$body,$matches);
嘿,我想我有办法了


您应该提供用于获取电子邮件的相应代码(不包括用户名和密码)。如果没有这些,就不可能帮助您,因为不知道您使用的函数/库是什么。
<?php
   $hostname = '{myserver/pop3/novalidate-cert}INBOX';
   $username = 'username';
   $password = 'password';

   /* try to connect */
   $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
   $msgno = 0; //message id
   $no_of_occurences = 0;
   $intStatic = 2;//to initialize the mail body section

   $decode = imap_fetchbody($mbox, $msgno , "");    
   $no_of_occurences = substr_count($decode,"Content-Transfer-Encoding: base64");//to get the no of images
   if($no_of_occurences > 0){
        for($i = 0; $i < $no_of_occurences; $i++){  
             $strChange =   strval($intStatic+$i);
             $decode = imap_fetchbody($mbox, $msgno , $strChange);//to get the base64 encoded string for the image
             $data = base64_decode($decode);
             $fName = time()."_".$strChange . '.gif';
             $file = $fName;
             $success = file_put_contents($file, $data);        //creates the physical image    
        }           
    }
    imap_close($inbox);
 ?>