我写了一个连接到我邮箱的课程,并将垃圾邮件移到我的垃圾文件夹中.它似乎没有工作,我不知道为什么.这是我有的:
<?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)进行关闭. 希望有所帮助,斯特凡