我编写了一个类来连接到我的邮箱并将垃圾邮件移动到我的垃圾文件夹。它似乎没有工作,但我不知道为什么。这是我所拥有的:
<?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 条消息。所以基本上它没有正确移动它们,因为它们没有从收件箱消失到垃圾箱中。有谁知道为什么?它正在获取所有消息并遍历它们,但似乎这一举动并没有发生。