/**
  * Creates pending incoming email from email message
  *
  * @param MailboxManagerEmail $email
  * @param IncomingMailbox $mailbox
  *
  * @return mixed
  */
 function createPendingEmail(&$email, &$mailbox)
 {
     if (!instance_of($email, 'MailboxManagerEmail')) {
         return new Error(lang('Email provided is empty'));
     }
     // if
     $incoming_mail = new IncomingMail();
     $incoming_mail->setProjectId($mailbox->getProjectId());
     $incoming_mail->setIncomingMailboxId($mailbox->getId());
     $incoming_mail->setHeaders($email->getHeaders());
     // object subject
     $subject = $email->getSubject();
     $incoming_mail->setSubject($subject);
     // object body
     $incoming_mail->setBody(incoming_mail_get_body($email));
     // object type and parent id
     $object_type = $mailbox->getObjectType();
     preg_match("/\\{ID(.*?)\\}(.*)/is", $subject, $results);
     if (count($results) > 0) {
         $parent_id = $results[1];
         $parent = ProjectObjects::findById($parent_id);
         if (instance_of($parent, 'ProjectObject') && $parent->can_have_comments) {
             $object_type = 'comment';
             $incoming_mail->setParentId($parent_id);
         } else {
             $incoming_mail->setParentId(null);
         }
         // if
         $subject = trim(str_replace($results[0], '', $subject));
         $incoming_mail->setSubject($subject);
     } else {
         //BOF:mod 20120809
         //echo $subject . "\n";
         $temp = explode('-', $subject);
         if (count($temp) == 1 || count($temp) == 2) {
             list($user_name, $priority) = $temp;
             $user_name = trim($user_name);
             $priority = trim($priority);
             //echo $user_name . ' | ' . $priority . "\n";
             $name_parts = explode(' ', $user_name);
             if (count($name_parts) == 1 || count($name_parts) == 2) {
                 list($first_name, $last_name) = $name_parts;
                 //echo $first_name . ' | ' . $last_name . "\n";
                 $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
                 mysql_select_db(DB_NAME, $link);
                 $query = "select id from healingcrystals_users where first_name='" . mysql_real_escape_string($first_name) . "' " . (!empty($last_name) ? " and last_name='" . mysql_real_escape_string($last_name) . "' " : "");
                 $result = mysql_query($query, $link);
                 if (mysql_num_rows($result)) {
                     $info = mysql_fetch_assoc($result);
                     $user_id = $info['id'];
                     $incoming_mail->setProjectId(TASK_LIST_PROJECT_ID);
                     $object_type = 'task';
                     //echo $user_id . ' | ' . $object_type . "\n";
                 }
                 mysql_close($link);
             }
         }
         if (empty($user_id)) {
             $temp_sender = $email->getAddress('from');
             if (is_array($temp_sender)) {
                 $temp_email_address = array_var($temp_sender, 'email', null);
                 $temp_user = Users::findByEmail($temp_email_address);
                 if (instance_of($temp_user, 'User')) {
                     $incoming_mail->setProjectId(TASK_LIST_PROJECT_ID);
                     $incoming_mail->setSubject($incoming_mail->getSubject() . '{' . $temp_user->getId() . '}');
                     $object_type = 'task';
                 }
             }
         }
         //EOF:mod 20120809
     }
     //BOF: mod
     if ($object_type == 'comment' && empty($parent_id)) {
         $object_type = 'ticket';
     }
     //EOF: mod
     $incoming_mail->setObjectType($object_type);
     if ($incoming_mail->getSubject() || $incoming_mail->getBody()) {
         if (!$incoming_mail->getSubject()) {
             $incoming_mail->setSubject(lang('[SUBJECT NOT PROVIDED]'));
         }
         // if
         if (!$incoming_mail->getBody() && in_array($incoming_mail->getObjectType(), array('comment', 'discussion'))) {
             $incoming_mail->setBody(lang('[CONTENT NOT PROVIDED]'));
         }
         // if
     }
     // if
     $sender = $email->getAddress('from');
     if (!is_array($sender)) {
         return new Error(lang('Sender is unknown'));
     }
     // if
     // user details
     $email_address = array_var($sender, 'email', null);
     $user = Users::findByEmail($email_address);
     if (!instance_of($user, 'User')) {
         $user = new AnonymousUser(array_var($sender, 'name', null) ? array_var($sender, 'name', null) : $email_address, $email_address);
     }
     // if
     $incoming_mail->setCreatedBy($user);
     // creation time
     $incoming_mail->setCreatedOn(new DateTimeValue());
     $result = $incoming_mail->save();
     if (!$result || is_error($result)) {
         return $result;
     }
     // if
     // create attachment objects
     $attachments = $email->getAttachments();
     if (is_foreachable($attachments)) {
         foreach ($attachments as $attachment) {
             $incoming_attachment = new IncomingMailAttachment();
             $incoming_attachment->setTemporaryFilename(basename(array_var($attachment, 'path', null)));
             $incoming_attachment->setOriginalFilename(array_var($attachment, 'filename', null));
             $incoming_attachment->setContentType(array_var($attachment, 'content_type', null));
             $incoming_attachment->setFileSize(array_var($attachment, 'size', null));
             $incoming_attachment->setMailId($incoming_mail->getId());
             $attachment_save = $incoming_attachment->save();
             if (!$attachment_save || is_error($attachment_save)) {
                 // we couldn't create object in database so we need to remove file from system
                 //@unlink(array_var($attachment,'path'));
             }
             // if
         }
         // foreach
     }
     // if
     return $incoming_mail;
 }