imap_mail_move无法正常工作

geo*_*310 4 php email imap

我写了一个连接到我邮箱的课程,并将垃圾邮件移到我的垃圾文件夹中.它似乎没有工作,我不知道为什么.这是我有的:

<?php
$cleaner = new Mail_cleaner();

echo 'Deleted '.$cleaner->deleted.' files';

Class Mail_cleaner {

public $server = '{mail.gridhost.co.uk:993/imap/ssl}';
public $folder = 'INBOX';
public $user = 'email@domain.com';
public $password = 'password';

public $mailbox;
public $check;
public $thelist;
public $overviews;
public $ids = array();
public $deleted = 0;

function __construct() {
    $this->open_connection();
    $this->get_messages();
}

function get_messages() {
    $this->ids = array();
    $this->overviews = imap_fetch_overview($this->mailbox,"1:{$this->check->Nmsgs}");

    foreach($this->overviews as $overview) {
        //print_r($overview); exit;
        if(stripos($overview->subject, 'SPAM')!==FALSE
        || stripos($overview->subject, 'Luxury Replicas')!==FALSE
        || stripos($overview->subject, 'Pharmacy')!==FALSE
        || stripos($overview->subject, 'viagra')!==FALSE
        || stripos($overview->subject, 'dr.maxman')!==FALSE
        || stripos($overview->subject, 'cialis')!==FALSE
        || stripos($overview->subject, 'penis enlarge')!==FALSE
        || stripos($overview->from, 'westin')!==FALSE
        || stripos($overview->from, 'rightmove')!==FALSE
        || stripos($overview->from, 'groupon')!==FALSE
        || stripos($overview->from, 'primelocation')!==FALSE
        || stripos($overview->from, 'mg-rover')!==FALSE
        ) {
            $this->ids[] = $overview->uid;

        }
    }
    if(count($this->ids) > 0) {
        $this->move_and_delete();
    }
}

function move_and_delete() {
    foreach($this->ids as $id) {
        // move to junk
        $result = imap_mail_move($this->mailbox, $id, 'INBOX.Junk');

        if($result) {
            //imap_delete($this->mailbox, $id); 
            $this->deleted++;
        }
    }
    imap_expunge($this->mailbox);
    imap_close($this->mailbox);
}

function open_connection() {
    $this->mailbox = imap_open($this->server.$this->folder, $this->user, $this->password);
    $this->check = imap_check($this->mailbox);
    $this->thelist = imap_getmailboxes($this->mailbox, $this->server, "*");
}

}
?>
Run Code Online (Sandbox Code Playgroud)

每次删除115条消息时,我得到相同的输出.如果我快速连续运行两次,那么第二次输出应删除0条消息.所以基本上它并没有正确地移动它们,因为它们并没有从收件箱中消失到垃圾箱中.谁知道为什么?它正在获取所有消息并循环通过它们,但似乎这一举动并没有发生.

Ext*_*Use 5

首先,我想知道您将UID添加到您的数组,但您尝试从消息序列中删除.您需要将选项参数(CP_UID)添加到imap_mail_move的调用中. $result = imap_mail_move($this->mailbox, $id, 'INBOX.Junk', CP_UID); 这也可能是你的删除不起作用的原因,因为它可能还没有移动任何消息.尝试期间$ result的价值是多少?由于您在结束时关闭了邮箱,因此可以通过使用来消除关闭imap_close($this->mailbox, CL_EXPUNGE).希望有所帮助,斯特凡