Email vault coming along. Implementing jQuery powered view for next commit

This commit is contained in:
Karl Cordes 2010-03-23 10:03:03 +11:00
parent 5622d8b94c
commit d1792506d2
7 changed files with 422 additions and 375 deletions

View file

@ -65,6 +65,8 @@ class EnquiriesController extends AppController {
* Fix up Email Attachment icon on email box. Currently broken -KC 6/8/09
$this->set('number_of_emails', $this->Enquiry->Email->find('count', array('conditions'=>array('Email.enquiry_id'=>$id))));
*/
$this->set('emails', $this->Enquiry->Email->find('all', array('conditions'=>array('Email.enquiry_id' => $id))));
$this->set('number_of_quotes', $this->Enquiry->Quote->find('count', array('conditions'=>array('Quote.enquiry_id'=>$id))));
//$this->set('number_of_files', $this->Enquiry->EnquiryFile->find('count', array('conditions' => array('EnquiryFile.enquiry_id'=>$id))));
$this->set('principle_emails', $this->Enquiry->Principle->PrincipleContact->findAllByPrincipleId($enquiry['Enquiry']['principle_id']));

View file

@ -5,7 +5,8 @@ class Email extends AppModel {
var $belongsTo = array(
'Enquiry' => array('className' => 'Enquiry',
'foreignKey' => 'enquiry_id'));
'foreignKey' => 'enquiry_id',
'counterCache' => true));
var $hasMany = array(
'EmailAttachment' => array('className' => 'EmailAttachment',

View file

@ -6,6 +6,7 @@ class EmailAttachment extends AppModel {
var $belongsTo = array(
'Email' => array('className' => 'Email',
'foreignKey' => 'email_id',
'counterCache' => true,
'conditions' => '',
'fields' => '',
'order' => ''

View file

@ -170,16 +170,19 @@ class Enquiry extends AppModel {
'counterQuery' => ''
),
'Email' => array('className' => 'Email',
'foreignKey' => 'enquiry_id',
'dependent' => false
),
/* 'EnquiryFile' => array('className' => 'EnquiryFile',
'foreignKey' => 'enquiry_id',
'dependent' => false
),*/
/*'Email' => array('className' => 'Email',
'foreignKey' => 'enquiry_id',
'dependent' => false
),
*/
);
var $hasOne = array('EnquiryEmailQueue');

View file

@ -17,11 +17,17 @@ class VaultShell extends Shell {
/* Setup Connection to the IMAP server */
$username = 'vault';
$password = 'xjdYOsmJWc37'; /* The password for the account to be checked */
$email_dir = '/var/www/cakephp/app/emails/working';
$email_dir = '/var/www/cakephp/app/emails';
$temp_filename = 'temp.eml';
$ripmime_path = '/usr/local/bin/ripmime';
if($testing == 1) {
$email_dir = '/Users/karlcordes/Sites/app/emails/working';
$email_dir = '/Users/karlcordes/Sites/quotenik/app/emails';
$ripmime_path = '/opt/local/bin/ripmime';
}
$mbox = imap_open("{mail.cmctechnologies.com.au:143}INBOX", $username, $password) or die("can't connect: " . imap_last_error());
@ -33,19 +39,24 @@ class VaultShell extends Shell {
if($number_of_messages == 0) {
exit(0);
}
exit(0);
/* Loop through the messages and sort them into ones to be processed or discarded */
for ($i=1; $i <= $number_of_messages; $i++) {
for ($i=30000; $i <= $number_of_messages; $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 \tSubject: ".$message['subject']."\n";
$enquiry = $this->checkIfValidEnquiry($message['subject'], $testing);
if($enquiry) {
echo "Found Enquiry number: ".$enquiry['Enquiry']['title']." Processing.\n";
//Process it and store the message and its attachments.
//Generate a Uniqid for this email.
$uniqid = $this->getUniqId($email_dir);
$this->Email->create();
$this->data['Email']['enquiry_id'] = $enquiry['Enquiry']['id'];
$this->data['Email']['to'] = $message['to'];
@ -55,27 +66,28 @@ class VaultShell extends Shell {
$this->data['Email']['subject'] = $message['subject'];
$this->data['Email']['body'] = "";
$this->data['Email']['plainbody'] = "";
$this->data['Email']['uniqid'] = $uniqid;
$structure = imap_fetchstructure($mbox, $i);
$attachments = 1;
if (empty($structure->parts)) { /* A single part message. No attachments and is plain text */
$this->data['Email']['body'] = imap_body($mbox, $i);
}
else {
$attachments = $this->fetchBodyAttachments($mbox, $i, $temp_filename, $email_dir);
$attachments = $this->fetchBodyAttachments($mbox, $i, $temp_filename, $email_dir, $uniqid, $ripmime_path);
if($attachments != 1) {
foreach ($attachments as $attachment) {
if($attachment['type'] == 'text/html') { //Assuming All HTML attachments are the body of the email
if(file_exists($email_dir.'/'.$attachment['name'])) {
$filecontents = file_get_contents($email_dir.'/'.$attachment['name']);
$size = filesize($email_dir.'/'.$attachment['name']);
if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name'])) {
$filecontents = file_get_contents($email_dir.'/'.$uniqid.'/'.$attachment['name']);
$size = filesize($email_dir.'/'.$uniqid.'/'.$attachment['name']);
$this->data['Email']['body'] .= $filecontents;
}
}
if($attachment['type'] == 'text/plain') { //Found plain text
if(file_exists($email_dir.'/'.$attachment['name'])) {
$filecontents = file_get_contents($email_dir.'/'.$attachment['name']);
$size = filesize($email_dir.'/'.$attachment['name']);
$filecontents = file_get_contents($email_dir.'/'.$uniqid.'/'.$attachment['name']);
$size = filesize($email_dir.'/'.$uniqid.'/'.$attachment['name']);
$this->data['Email']['plainbody'] .= $filecontents;
}
@ -92,7 +104,7 @@ class VaultShell extends Shell {
$email_id = $this->Email->id;
if($attachments != 1) {
foreach ($attachments as $attachment) {
if(file_exists($email_dir.'/'.$attachment['name']) != FALSE) {
if(file_exists($email_dir.'/'.$uniqid.'/'.$attachment['name']) != FALSE) {
if( ($attachment['type'] != 'text/html') && ($attachment['type'] != 'multipart/mixed') &&
($attachment['type'] != 'multipart/alternative') ) {
@ -101,7 +113,7 @@ class VaultShell extends Shell {
$this->data['EmailAttachment']['name'] = $attachment['name'];
$this->data['EmailAttachment']['type'] = $attachment['type'];
$this->data['EmailAttachment']['size'] = filesize($email_dir.'/'.$attachment['name']);
$this->data['EmailAttachment']['data'] = file_get_contents($email_dir.'/'.$attachment['name']);
$this->data['EmailAttachment']['filename'] = $email_dir.'/'.$uniqid.'/'.$attachment['name'];
if ($this->EmailAttachment->save($this->data)) {
echo "Saved file successfully to database\n";
}
@ -150,7 +162,7 @@ class VaultShell extends Shell {
}
$this->rmdirr($email_dir); //delete all attachments from the working directory. Easiest way to stop random files persisting
// $this->rmdirr($email_dir); //delete all attachments from the working directory. Easiest way to stop random files persisting
@ -191,11 +203,17 @@ class VaultShell extends Shell {
*
*/
function fetchBodyAttachments($mailbox, $msg_number, $filename, $email_dir) {
function fetchBodyAttachments($mailbox, $msg_number, $filename, $email_dir, $uniqid, $ripmime_path) {
$email_file = $email_dir.'/'.$filename;
$mkdirresult = mkdir ($email_dir.'/'.$uniqid);
if($mkdirresult == FALSE) {
echo "ERROR: Failed to make ".$email_dir.'/'.$uniqid;
return 1;
}
$email_file = $email_dir.'/'.$uniqid.'/'.$filename;
imap_savebody($mailbox, $email_file, $msg_number);
$command = "/usr/local/bin/ripmime -i $email_file -d $email_dir -v --verbose-contenttype --paranoid --prefix $msg_number";
$command = "$ripmime_path -i $email_file -d $email_dir/$uniqid -v --verbose-contenttype --paranoid --prefix $msg_number";
$output = array();
exec($command, $output, $status);
@ -237,7 +255,7 @@ class VaultShell extends Shell {
function getMessage($mbox, $msgnumber, $headers) {
$subject = $headers->subject;
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1");
$subject = iconv_mime_decode($subject, 0, "ISO-8859-1//IGNORE");
$date = $headers->date;
$recipients = $this->getRecipients($headers);
$message['subject'] = $subject;
@ -309,7 +327,7 @@ class VaultShell extends Shell {
if(isset($output[0])) { //Found a valid-looking Enquiry Number
$fetched_enquirynumber = $output[0];
echo $fetched_enquirynumber."\n";
// echo $fetched_enquirynumber."\n";
$enquiry = $this->Enquiry->findByTitle($fetched_enquirynumber);
if($enquiry) {
return $enquiry;
@ -359,5 +377,21 @@ class VaultShell extends Shell {
//rmdir($dirname);
}
/**
* Generate a uniq id.
* @todo Ensure the sub-directory doesn't exist to avoid collisions
*
*/
function getUniqId($email_dir) {
$uniqid = uniqid(null,TRUE);
//while(is_dir($email_dir."/".$uniqid) == TRUE) {
//
// }
return $uniqid;
}
}
?>

View file

@ -1,3 +1,5 @@
<?php if (!empty($emails)):?>
<table cellpadding = "0" cellspacing = "0" class="emailtable">
<tr>
@ -27,7 +29,7 @@
<tr<?php echo $class;?>>
<td><?php echo $email['Email']['date'];?></td>
<td><?php if(count($email['EmailAttachment']) > 0) {
<td><?php if($email['email_attachment_count'] > 0) {
echo $html->image('attach.png');
}
?>

View file

@ -1,6 +1,7 @@
<div class="enquiries-view">
<h2><?php __("Enquiry: " . $enquiry['Enquiry']['title']);?></h2>
<dl><?php $i = 0; $class = ' class="altrow"';?>
<dl><?php $i = 0;
$class = ' class="altrow"';?>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Enquiry Number'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
@ -85,7 +86,8 @@
echo $enquiry['ShippingAddress']['city'].' '.$shippingaddress['State']['name'].' '.$enquiry['ShippingAddress']['postcode'].'<br>';
echo '<b>'.$shippingaddress['Country']['name'].'</b>';
}
else { echo 'No Shipping Address Assigned for this Enquiry';
else {
echo 'No Shipping Address Assigned for this Enquiry';
}
?>
&nbsp;
@ -112,25 +114,13 @@ $boxdata['enquirynumber'] = $enquiry['Enquiry']['title'];
</div>
<div id="showemail">
</div>
<? /*
<div class="related">
<h3>
<?php __($number_of_emails.' Emails for this Enquiry');?><?php echo $html->image('internet-mail.png'); ?></h3>
<?php //echo $this->element('email_table_ajax', $enquiry); ?>
<?php echo $this->element('email_table_ajax', $enquiry, array('cache'=>'+1 day')); ?>
</div>
<div class="related">
<h3><?php __($number_of_files.' Files in this Enquiry');?><?php echo $html->image('document.png'); ?></h3>
<?php echo $this->element('enquiry_file_table', $files); ?>
</div>
*
*/
?>
*/?>
<div class="related">
<h3><?php __($number_of_quotes.' Quotes in this Enquiry');?></h3>
@ -138,4 +128,18 @@ $boxdata['enquirynumber'] = $enquiry['Enquiry']['title'];
</div>
<?php debug($quotes); ?>
<div id="showemail">
</div>
<div class="related">
<h3>
<?php __($enquiry['Enquiry']['email_count'].' Emails for this Enquiry');?><?php echo $html->image('internet-mail.png'); ?></h3>
<?php //echo $this->element('email_table_ajax', $enquiry); ?>
<?php echo $this->element('email_table_ajax', $enquiry, array('cache'=>'+1 day')); ?>
</div>
<?php //debug($emails); ?>