PHP IMAP函数分离附件并保存到目录中


PHP IMAP function to detach attachment and save to a directory

我正在尝试使用IMAP功能分离收件箱中收到的附件,并将其保存到指定的服务器目录中。

我对所有UNSEEN消息都这样做,但问题是它只对一条消息有效。

以下是代码(我删除了$host、$login、$password变量,原因很明显):

$type = 'ReadAttachment';
    $obj = new $type;
    $obj->getdata($host,$login,$password,$savedirpath,$delete_emails=false);
    class ReadAttachment
    {
        function getdecodevalue($message,$coding) {
            switch($coding) {
                case 0:
                case 1:
                    $message = imap_8bit($message);
                    break;
                case 2:
                    $message = imap_binary($message);
                    break;
                case 3:
                case 5:
                    $message = imap_base64($message);
                    break;
                case 4:
                    $message = imap_qprint($message);
                    break;
            }
            return $message;
        }

        function getdata($host,$login,$password,$savedirpath,$delete_emails=false, $read_type="UNSEEN") {
            // make sure save path has trailing slash (/)
            //print_r("test");
            $savedirpath = str_replace('''', '/', $savedirpath);
            if (substr($savedirpath, strlen($savedirpath) - 1) != '/') {
                $savedirpath .= '/';
            }
            $mbox = imap_open ($host, $login, $password) or die("can't connect: " . imap_last_error());
            $message = array();
            $message["attachment"]["type"][0] = "text";
            $message["attachment"]["type"][1] = "multipart";
            $message["attachment"]["type"][2] = "message";
            $message["attachment"]["type"][3] = "application";
            $message["attachment"]["type"][4] = "audio";
            $message["attachment"]["type"][5] = "image";
            $message["attachment"]["type"][6] = "video";
            $message["attachment"]["type"][7] = "other";
            //print_r($message);
            $emails = imap_search($mbox,$read_type) or die(print_r(imap_last_error()));
            print_r($emails);
            $e = imap_search($mbox,$read_type, SE_UID) or die(print_r(imap_last_error()));
            print_r($e);
            $i=0;
            foreach($emails as $email_number) {
                $structure = imap_fetchstructure($mbox, $e[$i] , FT_UID) or die(print_r(imap_last_error()));
                $parts = $structure->parts;
                $fpos=2;
                for($i = 1; $i < count($parts); $i++) {
                    $message["pid"][$i] = ($i);
                    $part = $parts[$i];
                    if($part->disposition == "attachment") {
                        $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                        $message["subtype"][$i] = strtolower($part->subtype);
                        $ext=$part->subtype;
                        $params = $part->dparameters;
                        $filename=$part->dparameters[0]->value;
                        $mege="";
                        $data="";
                        $mege = imap_fetchbody($mbox,$email_number,$fpos);  
                        $filename="$filename";
                        $fp=fopen($savedirpath.$filename,"w");
                        $data=$this->getdecodevalue($mege,$part->type);
                        //print_r($mege);
                        fputs($fp,$data);
                        fclose($fp);
                        $fpos+=1;
                    }
                }
                ++$i;
            }
            // imap_expunge deletes all tagged messages
            imap_close($mbox);
        }
    }

上面有什么我可以改变的吗?

我不确定这是否是问题的原因,或者我是否正确理解了您的代码。然而,我认为你的$I=0需要进入foreach循环,并且你需要在最后失去++$I。

让我们来看看。首先,你设置$i=0。foreach获得第一条消息并进入for循环,该循环迭代递增的$i值。假设当for循环结束时,$i被设置为4。此时,++$i将其设置为5。foreach迭代结束,应该处理下一封电子邮件。但这并没有发生,因为$i仍然是5,所以当你这样做时:

$structure = imap_fetchstructure($mbox, $e[$i] , FT_UID)

你选错了电子邮件。

实际上,从脚本来看,我认为他在两个地方错误地使用了$I。foreach中的$i应该是不同的变量。

对于$structure变量,他应该按预期使用$i。。。但是邮件部分应该使用一个单独的变量(我使用了$p)

我知道这是一个老帖子,但它帮了我一点忙。