php - imap_mail_move 不工作

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

}
?>

我每次说删除 115 条消息时都会得到相同的输出。如果我快速连续运行两次,那么第二次的输出应该被删除 0 条消息。所以基本上它没有正确移动它们,因为它们没有从收件箱中消失到垃圾箱中。有人知道为什么吗?它正在获取所有消息并循环遍历它们,但似乎没有发生任何变化。

最佳答案

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

关于php - imap_mail_move 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10298107/

相关文章:

通过 imap 访问 Facebook 消息

php - 当Mysql表中没有付款时,从php中的账单中减去付款

php - 在 Magento 中获取优惠券代码的客户名称

c# - 在电子邮件正文中动态获取收件人的电子邮件地址

java - 使用 JAVA 来自用户的电子邮件验证(简单)

ruby - 邮件gem判断是纯文本还是html

php - 使用 php 7.1 在 Ubuntu 14.04.2 LTS 上安装 php-imap

java - Android retrofit v2 插入失败错误403

php - 内存缓存在 xampp 中不起作用。找不到类 'Memcache'

ios - 如何在 iOS 中创建 gmail 收件人格式?