I wrote a class to connect to my mailbox and move spam to my junk mail folder. It seems to be working wrong, and I don't know why. Here is what I have:
<?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) {
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) {
$result = imap_mail_move($this->mailbox, $id, 'INBOX.Junk');
if($result) {
$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, "*");
}
}
?>
I get the same output every time I delete 115 posts. If I run it twice in a row, then 0 messages should be deleted a second time. Thus, basically this does not move them properly, because they do not disappear from the mailbox into the trash. Does anyone know why? He receives all the messages and looks through them, but it seems that the movement simply does not occur.
source
share