【问题标题】:Imap mail with phpImap 邮件与 php
【发布时间】:2015-05-02 20:49:34
【问题描述】:

我正在使用 IMAP/PHP 编写邮件客户端。但是我如何搜索带有附件的邮件。 imap_search() 没有选项

【问题讨论】:

    标签: php imap


    【解决方案1】:

    在 Imap/php 中有一个选项 imap_fetchstructure 它提供部分邮件类型

                $structure = imap_fetchstructure($inbox, $messageNumber);
                $flattenedParts = $this->flattenParts($structure->parts);
    
                foreach($flattenedParts as $partNumber => $part)
                {
                    switch($part->type)
                    {
    
                    case 0:
                        // the HTML or plain text part of the email
                        // now do something with the message, e.g. render it
                    break;
    
                    case 1:
                        // multi-part headers, can ignore
    
                    break;
                    case 2:
                        // attached message headers, can ignore
                    break;
    
                    case 3: // application
                    case 4: // audio
                        break;
                    case 5: // image
    
                        //break;
                    case 6: // video
                    case 7: // other
    
                           echo "Mail contain attachment ";
                            // don't know what it is
                        }
                        break;
    
                    }
                }
    
               function flattenParts ($messageParts,$flattenedParts=array(),$prefix='', $index = 1, $fullPrefix=true) 
                 {
    
        foreach($messageParts as $part) {
            $flattenedParts[$prefix.$index] = $part;
            if(isset($part->parts)) {
                if($part->type == 2) {
                    $flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix.$index.'.', 0, false);
                }
                elseif($fullPrefix) {
                    $flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix.$index.'.');
                }
                else {
                    $flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix);
                }
                unset($flattenedParts[$prefix.$index]->parts);
            }
            $index++;
        }
    
        return $flattenedParts;
    }
    

    【讨论】:

      【解决方案2】:

      如您所见,IMAP SEARCH command specification 没有搜索包含附件的邮件的选项,这不是 PHP 的怪癖。因此,您必须获取某个邮箱中可用邮件的列表,遍历它们并检查单个邮件是否有附件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 2011-05-21
        • 1970-01-01
        相关资源
        最近更新 更多