Nmsgs; echo $username."\n"; echo "Number of messages to Process ".$number_of_messages."\n"; if($number_of_messages == 0) { exit(0); } /* Find the strings we want to look for in the subjects. Build arrays * using the string as the keys, IDs as the value */ $enquiries = $this->Enquiry->find('all', array('recursive'=>0,'fields' => array('Enquiry.title', 'Enquiry.id'))); //$contacts = $this->Contact->find('all', array('recursive'=>0, 'fields'=>array('Contact.id', 'Contact.email'))); $invoices = $this->Invoice->find('all', array('recursive'=>0, 'fields'=>array('Invoice.id', 'Invoice.title'))); $purchaseOrders = $this->PurchaseOrder->find('all', array('recursive'=>0, 'fields'=>array('PurchaseOrder.id', 'PurchaseOrder.title'))); foreach ($enquiries as $enq) { $enqNumber = $enq['Enquiry']['title']; $id = $enq['Enquiry']['id']; $enquiryList[$enqNumber] = $id; } foreach ($invoices as $inv) { $invNumber = $inv['Invoice']['title']; $id = $inv['Invoice']['id']; $invoiceList[$invNumber] = $id; //echo $invNumber."\n"; } foreach ($purchaseOrders as $po) { $poNumber = $po['PurchaseOrder']['title']; $id = $po['PurchaseOrder']['id']; $poList[$poNumber] = $id; //echo $poNumber."\n"; } foreach($contacts as $contact) { $email = strtolower($contact['Contact']['email']); $id = $contact['Contact']['id']; $emailList[$email] = $id; //echo $email."\n"; } for($i=$number_of_messages; $i > 0; $i--) { $this_header = imap_headerinfo($mbox, $i); $message = $this->getMessage($mbox, $i, $this_header); // echo "Checking msg number: $i\tSubject: ".$message['subject']."\n"; //echo "Checking msg number: $i\n"; $enqID = $this->checkIfValidEnquiry($message['subject'], $enquiryList); if($enqID == false && $contactID == false) { $uid = imap_uid($mbox, $i); echo "Deleting msg number: $i\tSubject: ".$message['subject']."[{$uid}]"."\n"; imap_delete($mbox, $uid, 1); //$discardArray[] = $i; } else { $message['subject']." from ".$message['from']." will be saved"; } } imap_expunge($mbox); imap_close($mbox); } function checkIfValidEnquiry($subject, &$enqList) { $subject = iconv_mime_decode($subject, 0, "ISO-8859-1"); $output = array(); // $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1"); preg_match("/CMC\d+([NVQWSOT]|ACT|NT)E\d+-\d+/", $subject, $output); if(isset($output[0])) { //Found a valid-looking Enquiry Number $fetched_enquirynumber = $output[0]; echo "'$fetched_enquirynumber'\n"; if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists. $enqid = $enqList[$fetched_enquirynumber]; return $enqid; } else { return false; } } else { return false; } } function checkIfValidIdentifier($subject, &$list, $type) { $subject = iconv_mime_decode($subject, 0, "ISO-8859-1"); $output = array(); // $decoded_subject = iconv_mime_decode($subject, 2, "ISO-8859-1"); if($type == 'enquiry') { preg_match("/CMC\d+([NVQWSOT]|ACT|NT)E\d+-\d+/", $subject, $output); } else if ($type == 'invoice') { preg_match("/CMCIN\d+/", $subject, $output); } else if ($type == 'purchaseorder') { preg_match("/CMCPO\d+/", $subject, $output); } if(isset($output[0])) { //Found a valid-looking Enquiry Number $fetched_enquirynumber = $output[0]; echo "'$fetched_enquirynumber'\n"; if(array_key_exists($fetched_enquirynumber, $enqList)) { //check if it actually exists. $enqid = $enqList[$fetched_enquirynumber]; return $enqid; } else { return false; } } else { return false; } } function getMessage($mbox, $msgnumber, $headers) { $subject = $headers->subject; //$subject = iconv_mime_decode($subject, 0, "ISO-8859-1//IGNORE"); // $subject = mb_convert_encoding($subject, "ISO-8859-1"); // $subject = mb_convert_encoding($subject, "UTF-8"); $subject = mb_decode_mimeheader($subject); $date = $headers->date; $recipients = $this->getRecipients($headers, false); $message['subject'] = $subject; $message['date'] = $date; $message['to'] = $recipients['to']; $message['from'] = $recipients['from']; $message['cc'] = $recipients['cc']; return $message; } /** * get Recipients from the headers of an email * * if $returnArray is true, return an array rather than a string. * NOT USED DUE TO AWFUL WAY CONTACTS ARE SETUP. 3 different classes of people? WTF was I thinking. */ function getRecipients($headers, $returnArray = false) { $recipients = array(); if($returnArray == false) { $recipients['to'] = ""; $recipients['from'] = ""; $recipients['cc'] = ""; } else { $recipients['to'] = array(); $recipients['from'] = array(); $recipients['cc'] = array(); } if(isset($headers->to)) { $to = $headers->to; if(count($to) > 0) { foreach ($to as $id => $object) { if($returnArray == false) { $recipients['to'] .= "$object->mailbox@$object->host "; } else { $recipients['to'][] = strtolower($object->mailbox."@".$object->host); } } } } if(isset($headers->cc)) { $cc = $headers->cc; if(count($cc) > 0) { foreach ($cc as $id => $object) { if($returnArray == false) { $recipients['cc'] .= "$object->mailbox@$object->host "; } else { $recipients['cc'][] = strtolower($object->mailbox."@".$object->host); } } } } if(isset($headers->from)) { $from = $headers->from; if(count($from) > 0) { foreach ($from as $id => $object) { if($returnArray == false) { $recipients['from'] .= "$object->mailbox@$object->host"; } else { $recipients['from'][] = strtolower($object->mailbox."@".$object->host); } } } } return $recipients; } } ?>