printorlog("\n"); printorlog(date("Y-m-d H:i:s") . " Found " . count($msg_nums) . " emails."); $db = new db_alloc(); // fetch and parse email foreach ($msg_nums as $num) { // this will stream output flush(); $dir = ATTACHMENTS_DIR . "tmp"; if (!is_dir($dir)) { mkdir($dir); } usleep(25000); $x++; #$x>20 and alloc_error(fclose($fp).$mail->close()."\n"."Stopped."."\n"); $mail->set_msg($num); $decoded = $mail->save_email(); $body = trim(mime_parser::get_body_text($decoded)); $from = $decoded[0]["Headers"]["from:"]; $subject = $decoded[0]["Headers"]["subject:"]; $messageid = $decoded[0]["Headers"]["message-id:"]; $keys = $mail->get_hashes(); printorlog("\n"); printorlog($x . ". " . date("Y-m-d H:i:s") . " Keys:" . print_r($keys, 1) . " IMAP UID: " . $num); // Get a bunch of comments that look like they might belong to this email... unset($q); // First try and get a Key: from the subject line, or message-id $key = current($keys); if ($key && strlen($key) == 8) { $db->query("SELECT * FROM token WHERE tokenHash = '%s'", $key); $row = $db->row(); printorlog("Using key: " . $key . " relates to: " . $row["tokenEntity"] . ":" . $row["tokenEntityID"]);
} // Tests if (basename($_SERVER["PHP_SELF"]) == "email_receive.inc.php") { define("NO_AUTH", 1); require_once "alloc.php"; //require_once("emailsettings.php"); $num = 30; $e = new email_receive($info); $e->open_mailbox("INBOX"); echo "\nNum emails: " . $e->get_num_emails(); echo "\nNew emails: " . $e->get_num_new_emails(); echo "\ncheck_mail(): " . str_replace("\n", " ", print_r($e->mail_info, 1)); echo "\nget_new_email_msg_uids(): " . str_replace("\n", " ", print_r($e->get_new_email_msg_uids(), 1)); echo "\nget_all_email_msg_uids(): " . str_replace("\n", " ", print_r($e->get_all_email_msg_uids(), 1)); //exit(); echo "\nget_emails_UIDs_search(): " . str_replace("\n", " ", print_r($e->get_emails_UIDs_search("SUBJECT alloc"), 1)); //echo "\nget_msg_header(): ".str_replace("\n"," ",print_r($e->get_msg_header($num),1)); echo "\n"; $e->set_msg($num); $e->load_structure(); echo "\nload_structure(): " . str_replace(" ", " ", print_r($e->mail_structure, 1)); echo "\nget_charset(): " . $e->get_charset(); //exit(); list($h, $b) = $e->get_raw_email_by_msg_uid($num); //echo "\nget_raw_email_by_msg_uid(): "."HEADER: ".$h."\nBODY: ".$b; echo "\nsave_email(): " . $e->save_email(); //echo "\nload_parts(): ".print_r($e->mail_parts,1); echo "\nmail_text (plaintext version): " . $e->mail_text; echo "\nget_commands(): " . print_r($e->get_commands(task::get_exposed_fields()), 1); echo "\n"; }
function attach_email_to_existing_task($req = array()) { global $TPL; $info = inbox::get_mail_info(); $current_user =& singleton("current_user"); $orig_current_user =& $current_user; $req["taskID"] = sprintf("%d", $req["taskID"]); $task = new task(); $task->set_id($req["taskID"]); if ($task->select()) { $email_receive = new email_receive($info); $email_receive->open_mailbox($info["folder"]); $email_receive->set_msg($req["id"]); $email_receive->get_msg_header(); $email_receive->save_email(); $c = comment::add_comment_from_email($email_receive, $task); $commentID = $c->get_id(); $commentID and $TPL["message_good_no_esc"][] = "Created comment " . $commentID . " on task " . $task->get_task_link(array("prefixTaskID" => true)); // Possibly change the identity of current_user list($from_address, $from_name) = parse_email_address($email_receive->mail_headers["from"]); $person = new person(); $personID = $person->find_by_email($from_address); $personID or $personID = $person->find_by_name($from_name); if ($personID) { $current_user = new person(); $current_user->load_current_user($personID); singleton("current_user", $current_user); } // swap back to normal user $current_user =& $orig_current_user; singleton("current_user", $current_user); // manually add task manager and assignee to ip list $extraips = array(); if ($task->get_value("personID")) { $p = new person($task->get_value("personID")); if ($p->get_value("emailAddress")) { $extraips[$p->get_value("emailAddress")]["name"] = $p->get_name(); $extraips[$p->get_value("emailAddress")]["role"] = "assignee"; $extraips[$p->get_value("emailAddress")]["personID"] = $task->get_value("personID"); $extraips[$p->get_value("emailAddress")]["selected"] = 1; } } if ($task->get_value("managerID")) { $p = new person($task->get_value("managerID")); if ($p->get_value("emailAddress")) { $extraips[$p->get_value("emailAddress")]["name"] = $p->get_name(); $extraips[$p->get_value("emailAddress")]["role"] = "manager"; $extraips[$p->get_value("emailAddress")]["personID"] = $task->get_value("managerID"); $extraips[$p->get_value("emailAddress")]["selected"] = 1; } } // add all the other interested parties $ips = interestedParty::get_interested_parties("task", $req["taskID"], $extraips); foreach ((array) $ips as $k => $inf) { $inf["entity"] = "comment"; $inf["entityID"] = $commentID; $inf["email"] and $inf["emailAddress"] = $inf["email"]; if ($req["emailto"] == "internal" && !$inf["external"] && !$inf["clientContactID"]) { $id = interestedParty::add_interested_party($inf); $recipients[] = $inf["name"] . " " . add_brackets($k); } else { if ($req["emailto"] == "default") { $id = interestedParty::add_interested_party($inf); $recipients[] = $inf["name"] . " " . add_brackets($k); } } } $recipients and $recipients = implode(", ", (array) $recipients); $recipients and $TPL["message_good"][] = "Sent email to " . $recipients; // Re-email the comment out comment::send_comment($commentID, array("interested"), $email_receive); // File email away in the task's mail folder $mailbox = "INBOX/task" . $task->get_id(); $email_receive->create_mailbox($mailbox) and $TPL["message_good"][] = "Created mailbox: " . $mailbox; $email_receive->move_mail($req["id"], $mailbox) and $TPL["message_good"][] = "Moved email " . $req["id"] . " to " . $mailbox; $email_receive->close(); } }