Nmsgs; /* Loop through the messages and sort them into ones to be processed or discarded */ for ($i=1; $i <= $number_of_messages; $i++) { $this_header = imap_headerinfo($mbox, $i); preg_match("/CMC\d+([NVQWSO]|ACT|NT)E\d+-\d+/", $this_header->subject, $output); if(isset($output[0])) { $fetched_enquirynumber = $output[0]; $enquiry = $this->Enquiry->findByTitle($fetched_enquirynumber); if($enquiry['Enquiry']['id'] == NULL) { //It passes the Regex - but we don't find an Enquiry for it in the MER. imap_mail_move($mbox, $i, 'INBOX/Discarded'); //Discard it. } else { //Process it and store the message and its attachments. $message = $this->getMessage($mbox, $i, $this_header); $this->Email->create(); $this->data['Email']['enquiry_id'] = $enquiry['Enquiry']['id']; $this->data['Email']['to'] = $message['to']; $this->data['Email']['cc'] = $message['cc']; $this->data['Email']['from'] = $message['from']; $this->data['Email']['date'] = $message['date']; $this->data['Email']['subject'] = $message['subject']; $this->data['Email']['body'] = $message['body']; //Sanitize::clean($this->data); if($this->Email->save($this->data)) { $email_id = $this->Email->id; $attachment_files = $this->fetchAttachments($mbox, $i, $temp_filename, $email_dir, $attachment_dir); foreach ($attachment_files as $attachment) { $this->EmailAttachment->create(); $this->data['EmailAttachment']['email_id'] = $email_id; $this->data['EmailAttachment']['name'] = $attachment; $this->data['EmailAttachment']['type'] = mime_content_type("$attachment_dir/$attachment"); //Depreciated but works better than the new one $this->data['EmailAttachment']['size'] = filesize("$attachment_dir/$attachment"); $this->data['EmailAttachment']['data'] = fread(fopen("$attachment_dir/$attachment", "r"), $this->data['EmailAttachment']['size']); if ($this->EmailAttachment->save($this->data)) { echo "Saved file successfully to database\n"; } else { echo "Something went wrong saving the file to the DB\n"; } } echo "Email stored in the DB under enquiry ".$enquiry['Enquiry']['title']." Will be moved to the stored folder\n"; imap_mail_move($mbox, $i, 'INBOX/Stored'); //Move it to the stored folder. $this->clearEmailAttachmentDirs($email_dir, $temp_filename, $attachment_dir, $attachment_files); } else { echo 'Unable to save the Email\n'; } } } else { /* Can't find a valid-looking CMC Enquiry Number. Move the message to the discarded folder * I may change this to simply delete the emails. This will do for now, but it's doubling up on the storage for useless files. * */ if(imap_mail_move($mbox, $i, 'INBOX/Discarded')) { echo "Message Number $i contains no valid Enquiry number. Has been discarded\n"; } else { echo "Message Number $i contains no valid Enquiry number. But could not be discarded\n"; } } } /* Finished working with the IMAP server. Make the changes and close the connection */ imap_expunge($mbox); imap_close($mbox); } /* Fetches and Writes attachements to a directory under $email_dir. * Uses uudeview to do the decoding * */ function fetchAttachments($mailbox, $msg_number, $filename, $email_dir, $attachment_dir) { /* Make the temporary directory to store this email and the attachments */ if (!file_exists($email_dir)){ mkdir($email_dir); } if (!file_exists($attachment_dir)) { mkdir($attachment_dir); } $email_file = $email_dir.'/'.$filename; imap_savebody($mailbox, $email_file, $msg_number); $command = "uudeview -i $email_file -p $attachment_dir"; $output = array(); exec($command, $output); /* Check the $output array and find the filenames of the attachments */ $filenames = array(); for($i=0; $i< count($output); $i++) { $matches = preg_match("/\bsuccessfully\b/", $output[$i]); if($matches > 0) { $result = explode("/", $output[$i]); $filenames[] = $result[8]; // } } return $filenames; } /* clearEmailyAttachmentDirs($email_dir, $filename, $attachment_dir, $attachments) * * Deletes $email_dir/$filename.eml and the attachments specified in $attachment_dir * */ function clearEmailAttachmentDirs($email_dir, $filename, $attachment_dir, $attachments) { unlink("$email_dir/$filename"); foreach ($attachments as $attachment) { unlink("$attachment_dir/$attachment"); } } function getMessage($mbox, $msgnumber, $headers) { $subject = $headers->subject; $date = $headers->date; $recipients = $this->getRecipients($headers); $message['subject'] = $subject; $message['date'] = $date; $message['to'] = $recipients['to']; $message['from'] = $recipients['from']; $message['cc'] = $recipients['cc']; $message['body'] = $this->getBody($mbox, $msgnumber); return $message; } /* get Recipients from the headers of an email */ function getRecipients($headers) { $recipients = array(); $recipients['to'] = ""; $recipients['from'] = ""; $recipients['cc'] = ""; if(isset($headers->to)) { $to = $headers->to; if(count($to) > 0) { foreach ($to as $id => $object) { $recipients['to'] .= "$object->mailbox@$object->host "; } } } if(isset($headers->cc)) { $cc = $headers->cc; if(count($cc) > 0) { foreach ($cc as $id => $object) { $recipients['cc'] .= "$object->mailbox@$object->host "; } } } if(isset($headers->from)) { $from = $headers->from; if(count($from) > 0) { foreach ($from as $id => $object) { $recipients['from'] .= "$object->mailbox@$object->host"; } } } return $recipients; } function getBody($mbox, $msgnumber) { $structure = imap_fetchstructure($mbox, $msgnumber); if (!empty($structure->parts)) { $body = $this->getParts($structure->parts, $mbox, $msgnumber); } else { $body = imap_body($mbox, $msgnumber); /* Or get the plaintext */ } return $body; } function getParts($parts, $mbox, $msgnumber) { for ($i = 0, $j = count($parts); $i < $j; $i++) { $part = $parts[$i]; if ($part->subtype == 'HTML') { /* Find the HTML component of the body */ $body = imap_fetchbody($mbox, $msgnumber, $i+1); break; //Found HTML (preferred) So end the loop. } elseif($part->subtype == 'PLAIN') { $body = imap_fetchbody($mbox, $msgnumber, $i+1); } elseif(isset($part->parts)) { $body = $this->getParts($part->parts, $mbox, $msgnumber); /* Handle these goddamn Outlook messages */ } } return $body; } } ?>