如何在PHP中使用IMAP从电子邮件中提取内嵌图像(未附着)[闭](How to extract i

2019-07-20 08:39发布

当我使用的PHP IMAP电子邮件获取,我收到邮件正文内容但林不能够提取粘贴内嵌图像和电子邮件正文不附。

Answer 1:

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

假设你的电子邮件正文为$body ,那么你可以得到使用的preg_match图像的URL。 使用此的preg_match表达获得图像源。

preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $body, $matches);


Answer 2:

嘿,我想我有一个解决方案

<?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);
 ?>


文章来源: How to extract inline images(not attachment) from an email using imap in PHP [closed]
标签: php imap