<?php
class MyImapCleanup{
private $host;
private $port =110;
private $MyOpenMailBox;
private $debug;
private $adresses;
private $isDomainsToDel;
private $DomainsToDel;
private $isEmailsToDel;
private $EmailsToDel;
private $Headertags;
private $msgNrToDelete =array();
public function __construct($host,$debug= false) {
$this->host = $host;
$this->debug = $debug;
if($debug && !function_exists('imap_open')){
//die("PHP.INI =>uncoment ;extension=php_imap.dll");
}
}
public function loginpop3($user,$pwx){///novalidate-cert
$this->port=110;
if($this->debug){print"{".$this->host.":".$this->port."}".$user."*******";}//.$pwx};
return($this->MyOpenMailBox = imap_open("{".$this->host.":".$this->port."/pop3/novalidate-cert}",$user,$pwx));//or die(new Exception("connection failed"));
}
public function loginImap($user,$pwx){///novalidate-cert
$this->port=143;
if($this->debug){print"{".$this->host.":".$this->port."}".$user."*******";}//.$pwx};
return($this->MyOpenMailBox = imap_open("{".$this->host.":".$this->port."/novalidate-cert}INBOX",$user,$pwx));//or die(new Exception("connection failed"));
//$mbox = imap_open('{mail.domain.com:143/novalidate-cert}INBOX', 'user@domain.com', 'userpass')
}
public function loginImapb($user,$pwx){///novalidate-cert
$this->port=143;
if($this->debug){print"{".$this->host.":".$this->port."}".$user."*******";}//.$pwx};
return($this->MyOpenMailBox = imap_open("{".$this->host.":".$this->port."/imap}",$user,$pwx));//or die(new Exception("connection failed"));
}
public function setAdresses($array){
$this->adresses = $array;
}
public function setHeadertags($array){
$this->Headertags = $array;
}
public function checkForToDeleteEmails($start=1,$length){
$this->validateInputs();
if(isset($this->MyOpenMailBox)&&$headers = imap_check($this->MyOpenMailBox)){
$endnr =$headers->Nmsgs;
if($mails =imap_fetch_overview($this->MyOpenMailBox,"$start:$length",0)){
foreach($mails as $mailnr => $mailcontent){
if(++$mailnr>=0 && $Actiefmailheader = imap_header($this->MyOpenMailBox,$mailnr)){
if($this->MustEmailDeleteFromHead($Actiefmailheader)){
imap_delete($this->MyOpenMailBox,$mailnr);
$Actiefmailheader = imap_header($this->MyOpenMailBox,$mailnr);
//print($mailnr);
$this->debugprint($Actiefmailheader);
print "<h1>DELETE</h1>";
}else{
// print_r($Actiefmailheader->sender);
//$this->debugprint($Actiefmailheader);
}
if(isset($header->sender[0])){
$this->printHeader($Actiefmailheader);
}else{
print "<pre>".print_r($Actiefmailheader,true).print_r($mailcontent,true)."</pre>";
}
}
}
}
}
}
private function validateInputs(){
$this->isDomainsToDel = is_array($this->DomainsToDel);
$this->isEmailsToDel = is_array($this->EmailsToDel);
}
public function deleteSelection(){
imap_expunge($this->MyOpenMailBox);
}
public function MustEmailDeleteFromHead($header){
if(isset($header->sender[0])){
return $this->isDomainInCleaningList($header->sender[0]->host)||
$this->isEmailInCleaningList($header->sender[0]->mailbox."@".$header->sender[0]->host);
}else{
print_r(isset($header->sender[0]));
return false;
}
}
private function isDomainInCleaningList($domain){
return $this->isDomainsToDel && in_array($domain,$this->DomainsToDel);
}
private function isEmailInCleaningList($domain){
return $this->isEmailsToDel && in_array($domain,$this->EmailsToDel);
}
public function setEdomainsToDEL($domainsarray){
$this->DomainsToDel = $domainsarray;
}
public function setEMailsAdressesToDEL($emailarray){
$this->EmailsToDel = $emailarray;
}
public function deleteEmailFromAdresses(){
}
public function deleteEmailFromHeadertags(){
}
public function get(){
if(isset($this->MyOpenMailBox)){
$fstruct = imap_fetchstructure();
}
}
private function isSelectForDel($header){
return $header->Deleted == 'D';
}
private function debugprint($array){
print("<pre>");
print_r($array);
print("</pre>");
}
/**
* MyImap::__destruct()
* @return void
*/
public function __destruct() {
if(isset($this->MyOpenMailBox)){
imap_close($this->MyOpenMailBox);
}
}
public function printHeader($header){
print("<span> NR:".$header->Msgno."</span><span> ".(($this->isSelectForDel($header))?"DELETE":"")."</span><span> Mail:".$header->sender[0]->mailbox."@".$header->sender[0]->host."</span><br><span> Subject:".@$header->Subject."</span><br>");
}
}
?>