176 lines
6.2 KiB
PHP
176 lines
6.2 KiB
PHP
<?
|
|
App::import('Sanitize');
|
|
class VaultShell extends Shell {
|
|
/* vault.php - To be run in CLI via Cron. Fetches the mail from a certain mailbox, searches for a CMC Technologies Enquiry Numbeer
|
|
* then loads the data into the 'emails' table and the attachments in the 'email_attachments' table.
|
|
* Emails are discared or moved to a subfolder once processed
|
|
*/
|
|
|
|
|
|
var $uses = array('Enquiry', 'Email', 'EmailAttachments');
|
|
|
|
|
|
function main() {
|
|
/* Setup Connection to the IMAP server */
|
|
$username = 'vault';
|
|
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
|
|
$mbox = imap_open("{saturn:143/novalidate-cert}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
|
|
$MC = imap_check($mbox);
|
|
$number_of_messages = $MC->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.
|
|
echo $enquiry['Contact']['first_name'].' '.$enquiry['Contact']['last_name']."\n";
|
|
|
|
$this->Email->create();
|
|
$this->data['Email']['enquiry_id'] = $enquiry['Enquiry']['id'];
|
|
|
|
$this->data['Email']['to_address'] = $this_header->to[0]->mailbox.'@'.$this_header->to[0]->host;
|
|
|
|
$this->data['Email']['from_address'] = $this_header->from[0]->mailbox.'@'.$this_header->from[0]->host;
|
|
$this->data['Email']['to_name'] = $this_header->toaddress;
|
|
|
|
$this->data['Email']['from_name'] = $this_header->fromaddress;
|
|
$this->data['Email']['date'] = $this_header->date;
|
|
$this->data['Email']['subject'] = $this_header->subject;
|
|
$body = imap_fetchbody($mbox, $i, '1');
|
|
//$body = Sanitize::html($body, true);
|
|
$this->data['Email']['body'] = $body;
|
|
if($this->Email->save($this->data)) {
|
|
echo "Email stored in the DB under enquiry ".$enquiry['Enquiry']['title']."\n";
|
|
}
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
//Adapted from Code posted on the PHP manual by david at hundsness.com.
|
|
|
|
function getmsg($mbox,$mid) {
|
|
// input $mbox = IMAP stream, $mid = message id
|
|
// output all the following:
|
|
global $htmlmsg,$plainmsg,$charset,$attachments;
|
|
// the message may in $htmlmsg, $plainmsg, or both
|
|
$htmlmsg = $plainmsg = $charset = '';
|
|
$attachments = array();
|
|
|
|
// HEADER
|
|
$h = imap_header($mbox,$mid);
|
|
// add code here to get date, from, to, cc, subject...
|
|
|
|
// BODY
|
|
$s = imap_fetchstructure($mbox,$mid);
|
|
if (!$s->parts) // not multipart
|
|
getpart($mbox,$mid,$s,0); // no part-number, so pass 0
|
|
else { // multipart: iterate through each part
|
|
foreach ($s->parts as $partno0=>$p)
|
|
getpart($mbox,$mid,$p,$partno0+1);
|
|
}
|
|
}
|
|
|
|
function getpart($mbox,$mid,$p,$partno) {
|
|
// $partno = '1', '2', '2.1', '2.1.3', etc if multipart, 0 if not multipart
|
|
global $htmlmsg,$plainmsg,$charset,$attachments;
|
|
|
|
// DECODE DATA
|
|
$data = ($partno)?
|
|
imap_fetchbody($mbox,$mid,$partno): // multipart
|
|
imap_body($mbox,$mid); // not multipart
|
|
// Any part may be encoded, even plain text messages, so check everything.
|
|
if ($p->encoding==4)
|
|
$data = quoted_printable_decode($data);
|
|
elseif ($p->encoding==3)
|
|
$data = base64_decode($data);
|
|
// no need to decode 7-bit, 8-bit, or binary
|
|
|
|
// PARAMETERS
|
|
// get all parameters, like charset, filenames of attachments, etc.
|
|
$params = array();
|
|
if ($p->parameters)
|
|
foreach ($p->parameters as $x)
|
|
$params[ strtolower( $x->attribute ) ] = $x->value;
|
|
if ($p->dparameters)
|
|
foreach ($p->dparameters as $x)
|
|
$params[ strtolower( $x->attribute ) ] = $x->value;
|
|
|
|
// ATTACHMENT
|
|
// Any part with a filename is an attachment,
|
|
// so an attached text file (type 0) is not mistaken as the message.
|
|
if ($params['filename'] || $params['name']) {
|
|
// filename may be given as 'Filename' or 'Name' or both
|
|
$filename = ($params['filename'])? $params['filename'] : $params['name'];
|
|
// filename may be encoded, so see imap_mime_header_decode()
|
|
$attachments[$filename] = $data; // this is a problem if two files have same name
|
|
}
|
|
|
|
// TEXT
|
|
elseif ($p->type==0 && $data) {
|
|
// Messages may be split in different parts because of inline attachments,
|
|
// so append parts together with blank row.
|
|
if (strtolower($p->subtype)=='plain')
|
|
$plainmsg .= trim($data) ."\n\n";
|
|
else
|
|
$htmlmsg .= $data ."<br><br>";
|
|
$charset = $params['charset']; // assume all parts are same charset
|
|
}
|
|
|
|
// EMBEDDED MESSAGE
|
|
// Many bounce notifications embed the original message as type 2,
|
|
// but AOL uses type 1 (multipart), which is not handled here.
|
|
// There are no PHP functions to parse embedded messages,
|
|
// so this just appends the raw source to the main message.
|
|
elseif ($p->type==2 && $data) {
|
|
$plainmsg .= trim($data) ."\n\n";
|
|
}
|
|
|
|
// SUBPART RECURSION
|
|
if ($p->parts) {
|
|
foreach ($p->parts as $partno0=>$p2)
|
|
getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|