Skip to content

Commit

Permalink
[TASK|FUNCTION|FEATURE] Introduce ability to keyworkEntitie(s) to sav…
Browse files Browse the repository at this point in the history
…e original_email content

Basically the keyword entity can chose if the mails refering it should be saved with html or without.
The reason for this is because of some emails containing table and data depending on HTML tags, which if
stringidied makes them unreadable.

Resolves: #30
Related: #20, #19, #1
Releases: 1.2.x
  • Loading branch information
dud3 committed Nov 30, 2014
1 parent 12d7dae commit 6ad3416
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
19 changes: 18 additions & 1 deletion app/commands/readEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,24 @@ public function fire()

($_option_html_enable == "true") ? $_option_html_enable = true : $_option_html_enable = false;

$this->comment("Reading emails..." . "(" . $_option_email_search .")");
$this->comment("Reading Emails:");

$email_search_info = "Email search: " . $_option_email_search;
$email_search_info = str_pad($email_search_info, strlen($email_search_info) + 2, " ", STR_PAD_LEFT);

$this->info($email_search_info);

$email_html_info = "HTML enabled:";

if($_option_html_enable) {
$email_html_info = $email_html_info . " true";
} else {
$email_html_info = $email_html_info . " false";
}

$email_html_info = str_pad($email_html_info, strlen($email_html_info) + 2, " ", STR_PAD_LEFT);

$this->info($email_html_info);

echo "\n";

Expand Down
77 changes: 61 additions & 16 deletions app/repositories/EmailsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,17 @@ public function readMails($html_enable, $email_search) {
$std_email->body = $message->getMessageBody($html_enable);
$std_email->date = $message->getDate();

/* Check the subject fitst */
$std_email->subject = explode(" ", $std_email->subject);

/* If Fwd is present in the subject */
if(in_array('Fwd:', $std_email->subject)) {
unset($std_email->subject[0]);
}

$std_email->subject = implode(" ", $std_email->subject);

if(self::$enable_html_email || !self::$enable_html_email) {
if(!self::$enable_html_email) {

/* Explode the email into pieces */
$std_email->body = explode("\n", $std_email->body);
Expand All @@ -159,7 +161,6 @@ public function readMails($html_enable, $email_search) {
* -> keywords from the database even if we include the
* -> ^M symbol at the end of each array element.
*/

array_walk($std_email->body, array($this, 'trim_value'));

/**
Expand All @@ -174,35 +175,53 @@ public function readMails($html_enable, $email_search) {
* -> forwarded to an X person since we will forward the same email to multiple
* -> users that match the keyword(s), and replace their name on the emal.
*/

if(in_array('---------- Forwarded message ----------', $std_email->body)) {
$std_email->body = array_slice($std_email->body, 9);
}

/**
* if strpos($mystring, $findme)
* We might want to search the string
* -> if it contains the keyword of "Dear" or simmilar.
* We might want to search the string if it contains the keyword of "Dear" or simmilar.
*/

if(self::$enable_html_email) {
$this->search_for = ["Dear Alexander Notifications,<br /><br />", "", ""];
} else {
$this->search_for = ["Dear", "Dear Alexander", "Dear Alexander Notifications,"];
}

$this->search_for = ["Dear", "Dear Alexander", "Dear Alexander Notifications,"];

if(in_array($this->search_for[0], $std_email->body)
|| in_array($this->search_for[1], $std_email->body)
|| in_array($this->search_for[2], $std_email->body)) {

$std_email->body = array_slice($std_email->body, 3);
}

// Put everything all togather
$std_email->body = implode("\n", $std_email->body);
/**
* -----------------
* !If HTML enabled
* -----------------
*/
} else if(self::$enable_html_email) {

/* Explode the email into pieces */
$std_email->body = explode("\n", $std_email->body);

/* Repeat the simmilar as for non-HTML emails */
array_walk($std_email->body, array($this, 'trim_value'));

/* If the mail is forwarded in case */
if(in_array('---------- Forwarded message ----------', $std_email->body)) {
$std_email->body = array_slice($std_email->body, 9);
}

/* Search for default values in the mail */
$this->search_for = ["Dear Alexander Notifications,<br /><br />"];

if(in_array($this->search_for[0], $std_email->body)) {
$std_email->body = array_slice($std_email->body, 3);
}

}

/* Put everything all togather */
$std_email->body = implode("\n", $std_email->body);

$arr_emails[] = $std_email;

self::dump_output('headers', $std_email->header);
Expand All @@ -213,6 +232,9 @@ public function readMails($html_enable, $email_search) {

}

/**
* This one actualy does the magic for us.
*/
$this->getEmailKeywords($arr_emails);

self::closeDump();
Expand Down Expand Up @@ -386,7 +408,7 @@ public function getEmailKeywords($data) {

foreach ($data as $email) {

// Get the keywords from the DB
/* Get the keywords from the */
$get_keywords = explode(" ", $email->subject);
$k_db = keywords_list::all()->toArray();
$k_intersect = [];
Expand All @@ -399,6 +421,8 @@ public function getEmailKeywords($data) {
$k_db = trim($k_db);
$k_db = json_decode($k_db, true);

$k_db_original_content = (int)$db_keywords["original_content"];

/**
* Get the common between the database keywords that belong
* -> to each user and the keywords from the emails subject.
Expand Down Expand Up @@ -431,6 +455,7 @@ public function getEmailKeywords($data) {
*/
$k_arr_diff = array_diff($k_db, $get_keywords);

/*
echo "\n-------------------- DB --------------------\n";
var_dump($k_db);
echo "\n-------------------- Get --------------------\n";
Expand All @@ -439,9 +464,12 @@ public function getEmailKeywords($data) {
var_dump($k_intersect);
echo "\n-------------------- Diff Array --------------------\n";
var_dump($k_arr_diff);
*/

if(count($k_arr_diff) == 0) {

// echo ">>>>>>>>>>>>>>>>>>>>>>>> BINGO <<<<<<<<<<<<<<<<<<<<<<<<";

$e_add_list = email_address_list::where("keyword_id", "=", $k_id)->get()->toArray();

if($e_add_list !== null) {
Expand All @@ -453,6 +481,22 @@ public function getEmailKeywords($data) {
$std_store_email->full_name = $e_list["full_name"];
$std_store_email->subject = $email->subject;
$std_store_email->body = $email->body;
$std_store_email->html = 0;

/**
* Since we would want to distingush between the
* emails that depent on HTML we should first check if
* the email listener listens with html_enable = true
* and if they keywordEndtity wants to save the email in it's original content,
* if so flag the email.
*/
if(self::$enable_html_email) {
if($k_db_original_content == 1) {
$std_store_email->html = 1;
} else {
continue;
}
}

$std_store_email->__date = $email->header->date;
$std_store_email->__message_id = $email->header->message_id;
Expand Down Expand Up @@ -480,7 +524,7 @@ public function getEmailKeywords($data) {
* Put everything togather
* $std_store_email->body = implode("\n", $std_store_email->body);
*/

$this->storeMail($std_store_email);

}
Expand All @@ -506,6 +550,7 @@ public function storeMail($data) {
"email_address_id" => $data->email_address_id,
"subject" => $data->subject,
"body" => $data->body,
"html" => $data->html,

"x_message_id" => $data->__message_id,
"x_date" => $data->__date,
Expand Down

0 comments on commit 6ad3416

Please sign in to comment.