简体   繁体   English

使用php下载后imap“删除”消息?

[英]imap “delete” message after downloading using php?

Downloading attachment was working... but the delete wasn't? 正在下载附件正在工作...但是删除不是吗? Can anyone tell me whats wrong and how to make it work? 谁能告诉我有什么问题以及如何使它起作用?

<?php $hostname = '{xxxxxxpop3/notls}INBOX'; $username = 'xxx@gmail.com'; $password = 'password';

$savedirpath = "/root/Fax"; $type = 'ReadAttachment'; $obj = new $type; $obj->getdata($hostname, $username, $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 ($hostname, $username, $password, $savedirpath, $delete_emails = false)
    {
        // make sure savepath has trailing slash(/)         
        $savedirpath = str_replace('\\', '/', $savedirpath);
        if (substr($savedirpath, strlen($savedirpath) - 1) != '/')
        {
            $savedirpath .= '/';
        }
        $mbox = imap_open($hostname, $username, $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";
        for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++)
        {
            $structure = imap_fetchstructure($mbox, $jk, FT_UID);
            $parts = (isset($structure->parts) ? $structure->parts : false);
            $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, $jk, $fpos);
                    $filename = "$filename";
                    $fp = fopen($savedirpath . $filename, 'w');
                    $data = $this->getdecodevalue($mege, $part->type);
                    fputs($fp, $data);
                    fclose($fp);
                    $fpos += 1;
                }
            }
            if ($delete_emails)
            {
                // imap_delete tags a message for deletion
                imap_delete($mbox, $jk);
            }
        }        // imap_expunge deletes all tagged messages        if ($delete_emails) {           imap_expunge($mbox);        }
        imap_close($mbox);
    } }

?>

add

imap_expunge($jk); 

after

  imap_delete($mbox, $jk);

if it doesn't work try imap_errors to see whats going on. 如果不起作用,请尝试imap_errors看看发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM