加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – imap_mail_move无法正常工作

发布时间:2020-12-13 17:19:05 所属栏目:PHP教程 来源:网络整理
导读:我写了一个连接到我邮箱的课程,并将垃圾邮件移到我的垃圾文件夹中.它似乎没有工作,我不知道为什么.这是我有的: ?php$cleaner = new Mail_cleaner();echo 'Deleted '.$cleaner-deleted.' files';Class Mail_cleaner {public $server = '{mail.gridhost.co.uk
我写了一个连接到我邮箱的课程,并将垃圾邮件移到我的垃圾文件夹中.它似乎没有工作,我不知道为什么.这是我有的:

<?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,"*");
}

}
?>

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读