【问题标题】:How to extract inline images(not attachment) from an email using imap in PHP [closed]如何在 PHP 中使用 imap 从电子邮件中提取内联图像(不是附件)[关闭]
【发布时间】:2013-02-08 01:51:27
【问题描述】:

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

【问题讨论】:

  • 您应该提供用于获取电子邮件的相应代码(不包括用户名和密码)。否则,无法为您提供帮助,因为不知道您使用什么功能/库。

标签: php imap


【解决方案1】:

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

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

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

【讨论】:

    【解决方案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);
     ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2011-07-26
      相关资源
      最近更新 更多